From 8b75b77aac14e4351636060e446eca8725bdcf40 Mon Sep 17 00:00:00 2001 From: francis-wong Date: Sun, 11 Jun 2023 15:17:24 +0800 Subject: [PATCH] use RectangleSelector for callbacks and use getter functions for axes --- galleries/examples/event_handling/viewlims.py | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/galleries/examples/event_handling/viewlims.py b/galleries/examples/event_handling/viewlims.py index b47e3b5b0801..391447c132fa 100644 --- a/galleries/examples/event_handling/viewlims.py +++ b/galleries/examples/event_handling/viewlims.py @@ -3,7 +3,7 @@ Viewlims ======== -Creates two identical panels. Zooming in on the right panel will show +Creates two identical panels. Zooming in on the left panel will show a rectangle in the first panel, denoting the zoomed region. .. note:: @@ -17,21 +17,13 @@ import matplotlib.pyplot as plt 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 @@ -59,8 +51,12 @@ 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 + 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)) @@ -68,6 +64,20 @@ def ax_update(self, ax): 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.ax_update(ax2) + ax2.figure.canvas.draw_idle() + md = MandelbrotDisplay() Z = md.compute_image(-2., 0.5, -1.25, 1.25) @@ -77,17 +87,14 @@ 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( - [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) - -ax2.callbacks.connect('xlim_changed', md.ax_update) -ax2.callbacks.connect('ylim_changed', md.ax_update) -ax2.set_title("Zoom here") +rect_selector = RectangleSelector( + ax1, select_callback, + useblit=True, + button=[1], + interactive=True, + minspanx=5, + minspany=5, + spancoords='pixels') +ax1.set_title("Zoom here") plt.show()