Skip to content

Commit

Permalink
Restore zoom for 1D xarray plots
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Aug 27, 2024
1 parent 469a96f commit c295f8d
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions damnit/gui/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(
self.layout.addWidget(self._navigation_toolbar)

self._cursors = []
self._zoom_factory = None
self._scroll_zoom = None
self._panmanager = PanManager(self.figure, MouseButton.LEFT)

self.figure.tight_layout()
Expand All @@ -123,6 +123,13 @@ def _autoscale_enabled(self):
def _make_cursors(self):
return [] # Overridden in subclasses

def _setup_scroll_zoom(self):
# This needs to be redone when plotting changes the axes limits, so it
# zooms on the correct position.
if self._scroll_zoom is not None:
self._scroll_zoom() # Call to disconnect
self._scroll_zoom = zoom_factory(self._axis, base_scale=1.07)

def toggle_annotations(self, state):
if state == QtCore.Qt.Checked:
self._cursors.extend(self._make_cursors())
Expand Down Expand Up @@ -306,9 +313,7 @@ def update_canvas(self, xs, legend=None):
np.nanmin(x_all), np.nanmax(x_all), 0, np.nanmax(y_all), margin=0.05,
)

if self._zoom_factory is not None:
self._zoom_factory()
self._zoom_factory = zoom_factory(self._axis, base_scale=1.07)
self._setup_scroll_zoom()

# Update the toolbar history so that clicking the home button resets the
# plot limits properly.
Expand Down Expand Up @@ -393,9 +398,7 @@ def update_canvas(self, xs=None, ys=None, legend=None):

self.autoscale(xs_min, xs_max, ys_min, ys_max, margin=0.05)

if self._zoom_factory is not None:
self._zoom_factory()
self._zoom_factory = zoom_factory(self._axis, base_scale=1.07)
self._setup_scroll_zoom()

# Update the toolbar history so that clicking the home button resets the
# plot limits properly.
Expand All @@ -420,6 +423,7 @@ def __init__(self, parent, data, **kwargs):
self._nan_warning_label.show()

data.plot(ax=self._axis)
self._setup_scroll_zoom()
# The plot call above can add axis labels, so we need to do this again
self.figure.tight_layout()

Expand Down Expand Up @@ -490,9 +494,7 @@ def update_canvas(self, image):
vmax = np.nanquantile(image, 0.99, method='nearest')
self._image.set_clim(vmin, vmax)

if self._zoom_factory is not None:
self._zoom_factory()
self._zoom_factory = zoom_factory(self._axis, base_scale=1.07)
self._setup_scroll_zoom()

# Update the toolbar history so that clicking the home button resets the
# plot limits properly.
Expand Down

0 comments on commit c295f8d

Please sign in to comment.