8000 Use RectangleSelector for callbacks and getter functions for axes by francis-wong · Pull Request #26107 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Use RectangleSelector for callbacks and getter functions for axes #26107

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
51 changes: 31 additions & 20 deletions galleries/examples/event_handling/viewlims.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@
import numpy as np

from matplotlib.patches import Rectangle


# We just subclass Rectangle so that it can be called with an Axes
# instance, causing the rectangle to update its shape to match the
# bounds of the Axes
class UpdatingRect(Rectangle):
def __call__(self, ax):
self.set_bounds(*ax.viewLim.bounds)
ax.figure.canvas.draw_idle()

from matplotlib.widgets import RectangleSelector

# A class that will regenerate a fractal set as we zoom in, so that you
# can actually see the increasing detail. A box in the left panel will show
# the area to which we are zoomed.


class MandelbrotDisplay:
def __init__(self, h=500, w=500, niter=50, radius=2., power=2):
self.height = h
Expand Down Expand Up @@ -59,15 +52,35 @@ def ax_update(self, ax):
self.width, self.height = \
np.round(ax.patch.get_window_extent().size).astype(int)
# Get the range for the new area
vl = ax.viewLim
extent = vl.x0, vl.x1, vl.y0, vl.y1
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
extent = x0, x1, y0, y1
# Update the image object with our new data and extent
im = ax.images[-1]
im.set_data(self.compute_image(*extent))
im.set_extent(extent)
ax.figure.canvas.draw_idle()


def select_callback(eclick, erelease):
extent = rect_selector.extents

ax2.set_autoscale_on(False)
# Zoom the selected part
# Set xlim, ylim range for plot
# of rectangle selector box.
ax2.set_xlim(extent[0], extent[1])
ax2.set_ylim(extent[2], extent[3])

# update the right subplot
md = MandelbrotDisplay()
md.ax_update(ax2)
ax2.figure.canvas.draw_idle()

# update the rectangle box on the left subplot
rect.set_bounds(*ax2.viewLim.bounds)
ax1.figure.canvas.draw_idle()

md = MandelbrotDisplay()
Z = md.compute_image(-2., 0.5, -1.25, 1.25)

Expand All @@ -77,17 +90,15 @@ def ax_update(self, ax):
ax2.imshow(Z, origin='lower',
extent=(md.x.min(), md.x.max(), md.y.min(), md.y.max()))

rect = UpdatingRect(
rect = Rectangle(
[0, 0], 0, 0, facecolor='none', edgecolor='black', linewidth=1.0)
rect.set_bounds(*ax2.viewLim.bounds)
ax1.add_patch(rect)

# Connect for changing the view limits
ax2.callbacks.connect('xlim_changed', rect)
ax2.callbacks.connect('ylim_changed', rect)
rect_selector = RectangleSelector(
ax2, select_callback,
useblit=True,
button=[1],
spancoords='pixels')

ax2.callbacks.connect('xlim_changed', md.ax_update)
ax2.callbacks.connect('ylim_changed', md.ax_update)
ax2.set_title("Zoom here")

plt.show()
0