Skip to content

Commit 1c95f1b

Browse files
authored
Merge pull request matplotlib#30543 from LangQi99/feat/zoom
ENH: support x/y-axis zoom
2 parents 652012d + 0a744f0 commit 1c95f1b

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

doc/release/next_whats_new/scroll_to_zoom.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Zooming using mouse wheel
22
~~~~~~~~~~~~~~~~~~~~~~~~~
33

44
``Ctrl+MouseWheel`` can be used to zoom in the plot windows.
5+
Additionally, ``x+MouseWheel`` zooms only the x-axis and ``y+MouseWheel`` zooms only the y-axis.
56

6-
The zoom focusses on the mouse pointer, and keeps the aspect ratio of the axes.
7+
The zoom focusses on the mouse pointer. With ``Ctrl``, the axes aspect ratio is kept; with ``x`` or ``y``, only the respective axis is scaled.
78

89
Zooming is currently only supported on rectilinear Axes.

lib/matplotlib/backend_bases.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ def scroll_handler(event, canvas=None, toolbar=None):
25922592
# is required for interactive navigation.
25932593
return
25942594

2595-
if event.key == "control": # zoom towards the mouse position
2595+
if event.key in {"control", "x", "y"}: # zoom towards the mouse position
25962596
toolbar.push_current()
25972597

25982598
xmin, xmax = ax.get_xlim()
@@ -2604,10 +2604,21 @@ def scroll_handler(event, canvas=None, toolbar=None):
26042604
x, y = ax.transScale.transform((event.xdata, event.ydata))
26052605

26062606
scale_factor = 0.85 ** event.step
2607-
new_xmin = x - (x - xmin) * scale_factor
2608-
new_xmax = x + (xmax - x) * scale_factor
2609-
new_ymin = y - (y - ymin) * scale_factor
2610-
new_ymax = y + (ymax - y) * scale_factor
2607+
# Determine which axes to scale based on key
2608+
zoom_x = event.key in {"control", "x"}
2609+
zoom_y = event.key in {"control", "y"}
2610+
2611+
if zoom_x:
2612+
new_xmin = x - (x - xmin) * scale_factor
2613+
new_xmax = x + (xmax - x) * scale_factor
2614+
else:
2615+
new_xmin, new_xmax = xmin, xmax
2616+
2617+
if zoom_y:
2618+
new_ymin = y - (y - ymin) * scale_factor
2619+
new_ymax = y + (ymax - y) * scale_factor
2620+
else:
2621+
new_ymin, new_ymax = ymin, ymax
26112622

26122623
inv_scale = ax.transScale.inverted()
26132624
(new_xmin, new_ymin), (new_xmax, new_ymax) = inv_scale.transform(

0 commit comments

Comments
 (0)