-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
use RectangleSelector for callbacks and use getter functions for axes #26166
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||||||||||
Comment on lines
24
to
27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert
Suggested change
|
||||||||||||||||||||
def __init__(self, h=500, w=500, niter=50, radius=2., power=2): | ||||||||||||||||||||
self.height = h | ||||||||||||||||||||
|
@@ -59,15 +51,33 @@ 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 | ||||||||||||||||||||
Comment on lines
+54
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
# 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.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) | ||||||||||||||||||||
Comment on lines
-85
to
-87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see this part is missing, namely that zooming on the right Axes updates the selector on the left. |
||||||||||||||||||||
|
||||||||||||||||||||
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") | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did this change Axes to the left one? That is, if you mean to say, "use the rectangle selector", then maybe rename to "Select here" instead. |
||||||||||||||||||||
plt.show() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this changed; the rectangle is still on the left, and zooming occurs on the right.