Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Keep track of axes in interactive navigation. #9359

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 2 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Keep track of axes in interactive navigation.
  • Loading branch information
anntzer committed Oct 11, 2017
commit 2b0b376b724741b85e2b3b80d019f4adf201c560
58 changes: 26 additions & 32 deletions 58 lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2775,9 +2775,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 @@ -2799,19 +2797,24 @@ def __init__(self, canvas):
self.set_history_buttons()

@partial(canvas.mpl_connect, 'draw_event')
Copy link
Contributor

@dopplershift dopplershift Oct 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're processing the navigation stack on every draw event? I'm concerned about potential performance of all the extra set and stuff. I realize we're already slow, but kinda hoping to not make things worse.

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:
# Define the true initial navigation info.
self.push_current()
else:
axes, views, positions = nav_info
if axes != self.canvas.figure.axes:
# An axes has been added or removed, so update the
# navigation info too.
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 @@ -2830,15 +2833,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 @@ -3015,16 +3016,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)
axs = self.canvas.figure.axes
views = [ax._get_view() for ax in axs]
# Store both the original and modified positions.
positions = [
(ax.get_position(True).frozen(), ax.get_position().frozen())
for ax in axs]
self._nav_stack.push((axs, views, positions))
self.set_history_buttons()

def release(self, event):
Expand Down Expand Up @@ -3146,18 +3144,15 @@ def _update_view(self):
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
axs, views, pos = nav_info
for i, a in enumerate(self.canvas.figure.get_axes()):
a._set_view(views[i])
# 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()

def save_figure(self, *args):
Expand All @@ -3175,8 +3170,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 4 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.