Skip to content

Commit 28c765c

Browse files
timhoffmQuLogic
andcommitted
Scroll to zoom in screen coordinates
Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent 4e95402 commit 28c765c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,17 +2593,22 @@ def scroll_handler(event, canvas=None, toolbar=None):
25932593

25942594
xmin, xmax = ax.get_xlim()
25952595
ymin, ymax = ax.get_ylim()
2596+
xmin, ymin = ax.transScale.transform((xmin, ymin))
2597+
xmax, ymax = ax.transScale.transform((xmax, ymax))
25962598

2597-
# mouse position in data coordinates
2598-
x = event.xdata
2599-
y = event.ydata
2599+
# mouse position in scaled (e.g., log) data coordinates
2600+
x, y = ax.transScale.transform((event.xdata, event.ydata))
26002601

26012602
scale_factor = 1.0 - 0.05 * event.step
26022603
new_xmin = x - (x - xmin) * scale_factor
26032604
new_xmax = x + (xmax - x) * scale_factor
26042605
new_ymin = y - (y - ymin) * scale_factor
26052606
new_ymax = y + (ymax - y) * scale_factor
26062607

2608+
inv_scale = ax.transScale.inverted()
2609+
new_xmin, new_ymin = inv_scale.transform((new_xmin, new_ymin))
2610+
new_xmax, new_ymax = inv_scale.transform((new_xmax, new_ymax))
2611+
26072612
ax.set_xlim(new_xmin, new_xmax)
26082613
ax.set_ylim(new_ymin, new_ymax)
26092614

0 commit comments

Comments
 (0)