Skip to content
Merged
Show file tree
Hide file tree
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
142 changes: 50 additions & 92 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3828,18 +3828,15 @@ def _set_view_from_bbox(self, bbox, direction='in',
twiny : bool
Whether this axis is twinned in the *y*-direction.
"""
Xmin, Xmax = self.get_xlim()
Ymin, Ymax = self.get_ylim()

if len(bbox) == 3:
# Zooming code
xp, yp, scl = bbox
Xmin, Xmax = self.get_xlim()
Ymin, Ymax = self.get_ylim()

xp, yp, scl = bbox # Zooming code

# Should not happen
if scl == 0:
if scl == 0: # Should not happen
scl = 1.

# direction = 'in'
if scl > 1:
direction = 'in'
else:
Expand Down Expand Up @@ -3868,90 +3865,51 @@ def _set_view_from_bbox(self, bbox, direction='in',
"of length 3 or 4. Ignoring the view change.")
return

# Just grab bounding box
lastx, lasty, x, y = bbox

# zoom to rect
inverse = self.transData.inverted()
(lastx, lasty), (x, y) = inverse.transform([(lastx, lasty), (x, y)])

if twinx:
x0, x1 = Xmin, Xmax
else:
if Xmin < Xmax:
if x < lastx:
x0, x1 = x, lastx
else:
x0, x1 = lastx, x
if x0 < Xmin:
x0 = Xmin
if x1 > Xmax:
x1 = Xmax
else:
if x > lastx:
x0, x1 = x, lastx
else:
x0, x1 = lastx, x
if x0 > Xmin:
x0 = Xmin
if x1 < Xmax:
x1 = Xmax

if twiny:
y0, y1 = Ymin, Ymax
else:
if Ymin < Ymax:
if y < lasty:
y0, y1 = y, lasty
else:
y0, y1 = lasty, y
if y0 < Ymin:
y0 = Ymin
if y1 > Ymax:
y1 = Ymax
else:
if y > lasty:
y0, y1 = y, lasty
else:
y0, y1 = lasty, y
if y0 > Ymin:
y0 = Ymin
if y1 < Ymax:
y1 = Ymax

if direction == 'in':
if mode == 'x':
self.set_xlim((x0, x1))
elif mode == 'y':
self.set_ylim((y0, y1))
else:
self.set_xlim((x0, x1))
self.set_ylim((y0, y1))
elif direction == 'out':
if self.get_xscale() == 'log':
alpha = np.log(Xmax / Xmin) / np.log(x1 / x0)
rx1 = pow(Xmin / x0, alpha) * Xmin
rx2 = pow(Xmax / x0, alpha) * Xmin
else:
alpha = (Xmax - Xmin) / (x1 - x0)
rx1 = alpha * (Xmin - x0) + Xmin
rx2 = alpha * (Xmax - x0) + Xmin
if self.get_yscale() == 'log':
alpha = np.log(Ymax / Ymin) / np.log(y1 / y0)
ry1 = pow(Ymin / y0, alpha) * Ymin
ry2 = pow(Ymax / y0, alpha) * Ymin
else:
alpha = (Ymax - Ymin) / (y1 - y0)
ry1 = alpha * (Ymin - y0) + Ymin
ry2 = alpha * (Ymax - y0) + Ymin

if mode == 'x':
self.set_xlim((rx1, rx2))
elif mode == 'y':
self.set_ylim((ry1, ry2))
else:
self.set_xlim((rx1, rx2))
self.set_ylim((ry1, ry2))
# Original limits.
xmin0, xmax0 = self.get_xbound()
ymin0, ymax0 = self.get_ybound()
# The zoom box in screen coords.
startx, starty, stopx, stopy = bbox
# Convert to data coords.
(startx, starty), (stopx, stopy) = self.transData.inverted().transform(
[(startx, starty), (stopx, stopy)])
# Clip to axes limits.
xmin, xmax = np.clip(sorted([startx, stopx]), xmin0, xmax0)
ymin, ymax = np.clip(sorted([starty, stopy]), ymin0, ymax0)
# Don't double-zoom twinned axes or if zooming only the other axis.
if twinx or mode == "y":
xmin, xmax = xmin0, xmax0
if twiny or mode == "x":
ymin, ymax = ymin0, ymax0

if direction == "in":
new_xbound = xmin, xmax
new_ybound = ymin, ymax

elif direction == "out":
x_trf = self.xaxis.get_transform()
sxmin0, sxmax0, sxmin, sxmax = x_trf.transform(
[xmin0, xmax0, xmin, xmax]) # To screen space.
factor = (sxmax0 - sxmin0) / (sxmax - sxmin) # Unzoom factor.
# Move original bounds away by
# (factor) x (distance between unzoom box and axes bbox).
sxmin1 = sxmin0 - factor * (sxmin - sxmin0)
sxmax1 = sxmax0 + factor * (sxmax0 - sxmax)
# And back to data space.
new_xbound = x_trf.inverted().transform([sxmin1, sxmax1])

y_trf = self.yaxis.get_transform()
symin0, symax0, symin, symax = y_trf.transform(
[ymin0, ymax0, ymin, ymax])
factor = (symax0 - symin0) / (symax - symin)
symin1 = symin0 - factor * (symin - symin0)
symax1 = symax0 + factor * (symax0 - symax)
new_ybound = y_trf.inverted().transform([symin1, symax1])

if not twinx and mode != "y":
self.set_xbound(new_xbound)
if not twiny and mode != "x":
self.set_ybound(new_ybound)

def start_pan(self, x, y, button):
"""
Expand Down
47 changes: 46 additions & 1 deletion lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re

from matplotlib.backend_bases import (
FigureCanvasBase, LocationEvent, RendererBase)
FigureCanvasBase, LocationEvent, MouseButton, MouseEvent,
NavigationToolbar2, RendererBase)
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
import matplotlib.path as path
Expand Down Expand Up @@ -99,3 +100,47 @@ def test_location_event_position(x, y):
ax.format_coord(x, y))
ax.fmt_xdata = ax.fmt_ydata = lambda x: "foo"
assert re.match("x=foo +y=foo", ax.format_coord(x, y))


def test_interactive_zoom():
fig, ax = plt.subplots()
ax.set(xscale="logit")

class NT2(NavigationToolbar2):
def _init_toolbar(self): pass

tb = NT2(fig.canvas)
tb.zoom()

xlim0 = ax.get_xlim()
ylim0 = ax.get_ylim()

# Zoom from x=1e-6, y=0.1 to x=1-1e-5, 0.8 (data coordinates, "d").
d0 = (1e-6, 0.1)
d1 = (1-1e-5, 0.8)
# Convert to screen coordinates ("s"). Events are defined only with pixel
# precision, so round the pixel values, and below, check against the
# corresponding xdata/ydata, which are close but not equal to d0/d1.
s0 = ax.transData.transform(d0).astype(int)
s1 = ax.transData.transform(d1).astype(int)

# Zoom in.
start_event = MouseEvent(
"button_press_event", fig.canvas, *s0, MouseButton.LEFT)
fig.canvas.callbacks.process(start_event.name, start_event)
stop_event = MouseEvent(
"button_release_event", fig.canvas, *s1, MouseButton.LEFT)
fig.canvas.callbacks.process(stop_event.name, stop_event)
assert ax.get_xlim() == (start_event.xdata, stop_event.xdata)
assert ax.get_ylim() == (start_event.ydata, stop_event.ydata)

# Zoom out.
start_event = MouseEvent(
"button_press_event", fig.canvas, *s1, MouseButton.RIGHT)
fig.canvas.callbacks.process(start_event.name, start_event)
stop_event = MouseEvent(
"button_release_event", fig.canvas, *s0, MouseButton.RIGHT)
fig.canvas.callbacks.process(stop_event.name, stop_event)
# Absolute tolerance much less than original xmin (1e-7).
assert ax.get_xlim() == pytest.approx(xlim0, rel=0, abs=1e-10)
assert ax.get_ylim() == pytest.approx(ylim0, rel=0, abs=1e-10)