Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb80fe5

Browse files
committed
ENH: Add API to manage view state in custom Axes.
Custom Axes may require additional information than simply the x/y limits to describe the view. This API will allow the navigation buttons on the toolbar to function correctly with such Axes.
1 parent d69c7ac commit eb80fe5

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,6 +3132,40 @@ def set_navigate_mode(self, b):
31323132
"""
31333133
self._navigate_mode = b
31343134

3135+
def _get_view(self):
3136+
"""
3137+
Save information required to reproduce the current view.
3138+
3139+
Called before a view is changed, such as during a pan or zoom
3140+
initiated by the user. You may return any information you deem
3141+
necessary to describe the view.
3142+
3143+
.. note::
3144+
3145+
Intended to be overridden by new projection types, but if not, the
3146+
default implementation saves the view limits. You *must* implement
3147+
:meth:`_set_view` if you implement this method.
3148+
"""
3149+
xmin, xmax = self.get_xlim()
3150+
ymin, ymax = self.get_ylim()
3151+
return (xmin, xmax, ymin, ymax)
3152+
3153+
def _set_view(self, view):
3154+
"""
3155+
Apply a previously saved view.
3156+
3157+
Called when restoring a view, such as with the navigation buttons.
3158+
3159+
.. note::
3160+
3161+
Intended to be overridden by new projection types, but if not, the
3162+
default implementation restores the view limits. You *must*
3163+
implement :meth:`_get_view` if you implement this method.
3164+
"""
3165+
xmin, xmax, ymin, ymax = view
3166+
self.set_xlim((xmin, xmax))
3167+
self.set_ylim((ymin, ymax))
3168+
31353169
def start_pan(self, x, y, button):
31363170
"""
31373171
Called when a pan operation has started.

lib/matplotlib/backend_bases.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,17 +2949,15 @@ def _switch_off_zoom_mode(self, event):
29492949

29502950
def push_current(self):
29512951
"""push the current view limits and position onto the stack"""
2952-
lims = []
2952+
views = []
29532953
pos = []
29542954
for a in self.canvas.figure.get_axes():
2955-
xmin, xmax = a.get_xlim()
2956-
ymin, ymax = a.get_ylim()
2957-
lims.append((xmin, xmax, ymin, ymax))
2955+
views.append(a._get_view())
29582956
# Store both the original and modified positions
29592957
pos.append((
29602958
a.get_position(True).frozen(),
29612959
a.get_position().frozen()))
2962-
self._views.push(lims)
2960+
self._views.push(views)
29632961
self._positions.push(pos)
29642962
self.set_history_buttons()
29652963

@@ -3164,16 +3162,14 @@ def _update_view(self):
31643162
position stack for each axes
31653163
"""
31663164

3167-
lims = self._views()
3168-
if lims is None:
3165+
views = self._views()
3166+
if views is None:
31693167
return
31703168
pos = self._positions()
31713169
if pos is None:
31723170
return
31733171
for i, a in enumerate(self.canvas.figure.get_axes()):
3174-
xmin, xmax, ymin, ymax = lims[i]
3175-
a.set_xlim((xmin, xmax))
3176-
a.set_ylim((ymin, ymax))
3172+
a._set_view(views[i])
31773173
# Restore both the original and modified positions
31783174
a.set_position(pos[i][0], 'original')
31793175
a.set_position(pos[i][1], 'active')

lib/matplotlib/backend_tools.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,16 +466,14 @@ def update_view(self):
466466
position stack for each axes
467467
"""
468468

469-
lims = self.views[self.figure]()
470-
if lims is None:
469+
views = self.views[self.figure]()
470+
if views is None:
471471
return
472472
pos = self.positions[self.figure]()
473473
if pos is None:
474474
return
475475
for i, a in enumerate(self.figure.get_axes()):
476-
xmin, xmax, ymin, ymax = lims[i]
477-
a.set_xlim((xmin, xmax))
478-
a.set_ylim((ymin, ymax))
476+
a._set_view(views[i])
479477
# Restore both the original and modified positions
480478
a.set_position(pos[i][0], 'original')
481479
a.set_position(pos[i][1], 'active')
@@ -485,17 +483,15 @@ def update_view(self):
485483
def push_current(self):
486484
"""push the current view limits and position onto the stack"""
487485

488-
lims = []
486+
views = []
489487
pos = []
490488
for a in self.figure.get_axes():
491-
xmin, xmax = a.get_xlim()
492-
ymin, ymax = a.get_ylim()
493-
lims.append((xmin, xmax, ymin, ymax))
489+
views.append(a._get_view())
494490
# Store both the original and modified positions
495491
pos.append((
496492
a.get_position(True).frozen(),
497493
a.get_position().frozen()))
498-
self.views[self.figure].push(lims)
494+
self.views[self.figure].push(views)
499495
self.positions[self.figure].push(pos)
500496

501497
def refresh_locators(self):

0 commit comments

Comments
 (0)