Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support panning and zooming using mouse scroll wheel #20317

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
39 changes: 39 additions & 0 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,42 @@ def button_press_handler(event, canvas=None, toolbar=None):
toolbar.forward()


def scroll_handler(event, canvas=None, toolbar=None):
ax = event.inaxes
if ax is None:
return

if toolbar is None:
if canvas is None:
canvas = event.canvas
toolbar = canvas.toolbar

if toolbar is None or toolbar.mode == _Mode.NONE:
return

if event.key is None: # vertical scroll
toolbar.push_current()
ymin, ymax = ax.get_ylim()
delta = 0.05 * event.step * (ymax - ymin)
ax.set_ylim(ymin + delta, ymax + delta)
ax.figure.canvas.draw_idle()
elif event.key == 'shift': # horizontal scroll
toolbar.push_current()
xmin, xmax = ax.get_xlim()
delta = 0.05 * event.step * (xmax - xmin)
ax.set_xlim(xmin + delta, xmax + delta)
ax.figure.canvas.draw_idle()
elif event.key == 'control': # zoom
toolbar.push_current()
xmin, xmax = ax.get_xlim()
delta = 0.05 * event.step * (xmax - xmin)
ax.set_xlim(xmin + delta, xmax - delta)
ymin, ymax = ax.get_ylim()
delta = 0.05 * event.step * (ymax - ymin)
ax.set_ylim(ymin + delta, ymax - delta)
ax.figure.canvas.draw_idle()


class NonGuiException(Exception):
"""Raised when trying show a figure in a non-GUI backend."""
pass
Expand Down Expand Up @@ -2760,11 +2796,14 @@ def __init__(self, canvas, num):

self.key_press_handler_id = None
self.button_press_handler_id = None
self.scroll_handler_id = None
if rcParams['toolbar'] != 'toolmanager':
self.key_press_handler_id = self.canvas.mpl_connect(
'key_press_event', key_press_handler)
self.button_press_handler_id = self.canvas.mpl_connect(
'button_press_event', button_press_handler)
self.scroll_handler_id = self.canvas.mpl_connect(
'scroll_event', scroll_handler)

self.toolmanager = (ToolManager(canvas.figure)
if mpl.rcParams['toolbar'] == 'toolmanager'
Expand Down