Skip to content

Toolbar tracks views if axes are added during use #3347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 80 additions & 18 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,7 @@ def __init__(self, canvas):
# at start

self.mode = '' # a mode string for the status bar
self._home_views = {}
self.set_history_buttons()

def set_message(self, s):
Expand All @@ -2691,6 +2692,7 @@ def set_message(self, s):

def back(self, *args):
"""move back up the view lim stack"""
self._update_home_views()
self._views.back()
self._positions.back()
self.set_history_buttons()
Expand All @@ -2705,13 +2707,15 @@ def draw_rubberband(self, event, x0, y0, x1, y1):

def forward(self, *args):
"""Move forward in the view lim stack"""
self._update_home_views()
self._views.forward()
self._positions.forward()
self.set_history_buttons()
self._update_view()

def home(self, *args):
"""Restore the original view"""
self._update_home_views()
self._views.home()
self._positions.home()
self.set_history_buttons()
Expand Down Expand Up @@ -2889,21 +2893,67 @@ def _switch_off_zoom_mode(self, event):
self.mouse_move(event)

def push_current(self):
"""push the current view limits and position onto the stack"""
lims = []
pos = []
"""
Push the current view limits and position onto their respective stacks
"""

lims = {}
pos = {}
for a in self.canvas.figure.get_axes():
xmin, xmax = a.get_xlim()
ymin, ymax = a.get_ylim()
lims.append((xmin, xmax, ymin, ymax))
# Store both the original and modified positions
pos.append((
a.get_position(True).frozen(),
a.get_position().frozen()))
lims[a] = self._axes_view(a)
pos[a] = self._axes_pos(a)
self._views.push(lims)
self._positions.push(pos)
self._update_home_views()
self.set_history_buttons()

def _axes_view(self, ax):
"""
Return the current view limits for the specified axes

Parameters
----------
ax : (matplotlib.axes.AxesSubplot)
The axes to get the view limits for

Returns
-------
limits : (tuple)
A tuple of the view limits
"""

xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
return (xmin, xmax, ymin, ymax)

def _axes_pos(self, ax):
"""
Return the original and modified positions for the specified axes

Parameters
----------
ax : (matplotlib.axes.AxesSubplot)
The axes to get the positions for

Returns
-------
limits : (tuple)
A tuple of the original and modified positions
"""

return (ax.get_position(True).frozen(),
ax.get_position().frozen())

def _update_home_views(self):
"""
Make sure that self._home_views has an entry for all axes present in the
figure
"""

for a in self.canvas.figure.get_axes():
if a not in self._home_views:
self._home_views[a] = self._axes_view(a)

def release(self, event):
"""this will be called whenever mouse button is released"""
pass
Expand Down Expand Up @@ -3101,8 +3151,11 @@ def draw(self):
self.canvas.draw_idle()

def _update_view(self):
"""Update the viewlim and position from the view and
position stack for each axes
"""
Update the view limits and position for each axes from the current stack
position. If any axes are present in the figure that aren't in the
current stack position, use the home view limits for those axes and
don't update *any* positions.
"""

lims = self._views()
Expand All @@ -3111,15 +3164,23 @@ def _update_view(self):
pos = self._positions()
if pos is None:
return
for i, a in enumerate(self.canvas.figure.get_axes()):
xmin, xmax, ymin, ymax = lims[i]
all_axes = self.canvas.figure.get_axes()
for a in all_axes:
if a in lims:
cur_lim = lims[a]
else:
cur_lim = self._home_views[a]
xmin, xmax, ymin, ymax = cur_lim
a.set_xlim((xmin, xmax))
a.set_ylim((ymin, ymax))
# Restore both the original and modified positions
a.set_position(pos[i][0], 'original')
a.set_position(pos[i][1], 'active')

self.canvas.draw_idle()
if set(all_axes).issubset(pos.keys()):
for a in all_axes:
# Restore both the original and modified positions
a.set_position(pos[a][0], 'original')
a.set_position(pos[a][1], 'active')

self.draw()

def save_figure(self, *args):
"""Save the current figure"""
Expand All @@ -3134,6 +3195,7 @@ def set_cursor(self, cursor):

def update(self):
"""Reset the axes stack"""
self._home_views.clear()
self._views.clear()
self._positions.clear()
self.set_history_buttons()
Expand Down