Skip to content

Backport PR #9359 on branch v2.1.x #9632

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

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
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
66 changes: 29 additions & 37 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import sys
import time
import warnings
from weakref import WeakKeyDictionary

import numpy as np
import matplotlib.cbook as cbook
Expand Down Expand Up @@ -2781,9 +2782,7 @@ class NavigationToolbar2(object):
def __init__(self, canvas):
self.canvas = canvas
canvas.toolbar = self
# a dict from axes index to a list of view limits
self._views = cbook.Stack()
self._positions = cbook.Stack() # stack of subplot positions
self._nav_stack = cbook.Stack()
self._xypress = None # the location and axis info at the time
# of the press
self._idPress = None
Expand All @@ -2805,19 +2804,20 @@ def __init__(self, canvas):
self.set_history_buttons()

@partial(canvas.mpl_connect, 'draw_event')
def define_home(event):
self.push_current()
# The decorator sets `define_home` to the callback cid, so we can
# disconnect it after the first use.
canvas.mpl_disconnect(define_home)
def update_stack(event):
nav_info = self._nav_stack()
if (nav_info is None # True initial navigation info.
# An axes has been added or removed, so update the
# navigation info too.
or set(nav_info) != set(self.canvas.figure.axes)):
self.push_current()

def set_message(self, s):
"""Display a message on toolbar or in status bar."""

def back(self, *args):
"""move back up the view lim stack"""
self._views.back()
self._positions.back()
self._nav_stack.back()
self.set_history_buttons()
self._update_view()

Expand All @@ -2836,15 +2836,13 @@ def remove_rubberband(self):

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

def home(self, *args):
"""Restore the original view."""
self._views.home()
self._positions.home()
self._nav_stack.home()
self.set_history_buttons()
self._update_view()

Expand Down Expand Up @@ -3021,16 +3019,13 @@ def _switch_off_zoom_mode(self, event):

def push_current(self):
"""Push the current view limits and position onto the stack."""
views = []
pos = []
for a in self.canvas.figure.get_axes():
views.append(a._get_view())
# Store both the original and modified positions
pos.append((
a.get_position(True).frozen(),
a.get_position().frozen()))
self._views.push(views)
self._positions.push(pos)
self._nav_stack.push(
WeakKeyDictionary(
{ax: (ax._get_view(),
# Store both the original and modified positions.
(ax.get_position(True).frozen(),
ax.get_position().frozen()))
for ax in self.canvas.figure.axes}))
self.set_history_buttons()

def release(self, event):
Expand Down Expand Up @@ -3151,19 +3146,17 @@ def _update_view(self):
"""Update the viewlim and position from the view and
position stack for each axes.
"""

views = self._views()
if views is None:
return
pos = self._positions()
if pos is None:
nav_info = self._nav_stack()
if nav_info is None:
return
for i, a in enumerate(self.canvas.figure.get_axes()):
a._set_view(views[i])
# Retrieve all items at once to avoid any risk of GC deleting an Axes
# while in the middle of the loop below.
items = list(nav_info.items())
for ax, (view, (pos_orig, pos_active)) in items:
ax._set_view(view)
# Restore both the original and modified positions
a.set_position(pos[i][0], 'original')
a.set_position(pos[i][1], 'active')

ax.set_position(pos_orig, 'original')
ax.set_position(pos_active, 'active')
self.canvas.draw_idle()

def save_figure(self, *args):
Expand All @@ -3181,8 +3174,7 @@ def set_cursor(self, cursor):

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

def zoom(self, *args):
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1703,8 +1703,8 @@ def set_message(self, s):
self.statbar.set_function(s)

def set_history_buttons(self):
can_backward = (self._views._pos > 0)
can_forward = (self._views._pos < len(self._views._elements) - 1)
can_backward = self._nav_stack._pos > 0
can_forward = self._nav_stack._pos < len(self._nav_stack._elements) - 1
self.EnableTool(self.wx_ids['Back'], can_backward)
self.EnableTool(self.wx_ids['Forward'], can_forward)

Expand Down