Skip to content

Commit c6151a3

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 a1f96a5 commit c6151a3

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

lib/matplotlib/axes/_base.py

+29
Original file line numberDiff line numberDiff line change
@@ -3132,6 +3132,35 @@ def set_navigate_mode(self, b):
31323132
"""
31333133
self._navigate_mode = b
31343134

3135+
def save_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.
3141+
3142+
.. note::
3143+
3144+
Intended to be overridden by new projection types, but if not, the
3145+
default implementation saves the view limits. You *must* implement
3146+
:meth:`restore_view` if you implement this method.
3147+
"""
3148+
raise NotImplementedError
3149+
3150+
def restore_view(self, view):
3151+
"""
3152+
Apply a previously saved view.
3153+
3154+
Called when restoring a view, such as with the navigation buttons.
3155+
3156+
.. note::
3157+
3158+
Intended to be overridden by new projection types, but if not, the
3159+
default implementation restores the view limits. You *must*
3160+
implement :meth:`save_view` if you implement this method.
3161+
"""
3162+
raise NotImplementedError
3163+
31353164
def start_pan(self, x, y, button):
31363165
"""
31373166
Called when a pan operation has started.

lib/matplotlib/backend_bases.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -2953,9 +2953,14 @@ def push_current(self):
29532953
lims = []
29542954
pos = []
29552955
for a in self.canvas.figure.get_axes():
2956-
xmin, xmax = a.get_xlim()
2957-
ymin, ymax = a.get_ylim()
2958-
lims.append((xmin, xmax, ymin, ymax))
2956+
try:
2957+
view = a.save_view()
2958+
except NotImplementedError:
2959+
xmin, xmax = a.get_xlim()
2960+
ymin, ymax = a.get_ylim()
2961+
lims.append((xmin, xmax, ymin, ymax))
2962+
else:
2963+
lims.append(view)
29592964
# Store both the original and modified positions
29602965
pos.append((
29612966
a.get_position(True).frozen(),
@@ -3172,9 +3177,12 @@ def _update_view(self):
31723177
if pos is None:
31733178
return
31743179
for i, a in enumerate(self.canvas.figure.get_axes()):
3175-
xmin, xmax, ymin, ymax = lims[i]
3176-
a.set_xlim((xmin, xmax))
3177-
a.set_ylim((ymin, ymax))
3180+
try:
3181+
a.restore_view(lims[i])
3182+
except NotImplementedError:
3183+
xmin, xmax, ymin, ymax = lims[i]
3184+
a.set_xlim((xmin, xmax))
3185+
a.set_ylim((ymin, ymax))
31783186
# Restore both the original and modified positions
31793187
a.set_position(pos[i][0], 'original')
31803188
a.set_position(pos[i][1], 'active')

lib/matplotlib/backend_tools.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,12 @@ def update_view(self):
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+
try:
477+
a.restore_view(lims[i])
478+
except NotImplementedError:
479+
xmin, xmax, ymin, ymax = lims[i]
480+
a.set_xlim((xmin, xmax))
481+
a.set_ylim((ymin, ymax))
479482
# Restore both the original and modified positions
480483
a.set_position(pos[i][0], 'original')
481484
a.set_position(pos[i][1], 'active')
@@ -488,9 +491,14 @@ def push_current(self):
488491
lims = []
489492
pos = []
490493
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))
494+
try:
495+
view = a.save_view()
496+
except NotImplementedError:
497+
xmin, xmax = a.get_xlim()
498+
ymin, ymax = a.get_ylim()
499+
lims.append((xmin, xmax, ymin, ymax))
500+
else:
501+
lims.append(view)
494502
# Store both the original and modified positions
495503
pos.append((
496504
a.get_position(True).frozen(),

0 commit comments

Comments
 (0)