@@ -2574,6 +2574,51 @@ def button_press_handler(event, canvas=None, toolbar=None):
2574
2574
toolbar .forward ()
2575
2575
2576
2576
2577
+ def scroll_handler (event , canvas = None , toolbar = None ):
2578
+ ax = event .inaxes
2579
+ if ax is None :
2580
+ return
2581
+ if ax .name != "rectilinear" :
2582
+ # zooming is currently only supported on rectilinear axes
2583
+ return
2584
+
2585
+ if toolbar is None :
2586
+ toolbar = (canvas or event .canvas ).toolbar
2587
+
2588
+ if toolbar is None :
2589
+ # technically we do not need a toolbar, but until wheel zoom was
2590
+ # introduced, any interactive modification was only possible through
2591
+ # the toolbar tools. For now, we keep the restriction that a toolbar
2592
+ # is required for interactive navigation.
2593
+ return
2594
+
2595
+ if event .key == "control" : # zoom towards the mouse position
2596
+ toolbar .push_current ()
2597
+
2598
+ xmin , xmax = ax .get_xlim ()
2599
+ ymin , ymax = ax .get_ylim ()
2600
+ (xmin , ymin ), (xmax , ymax ) = ax .transScale .transform (
2601
+ [(xmin , ymin ), (xmax , ymax )])
2602
+
2603
+ # mouse position in scaled (e.g., log) data coordinates
2604
+ x , y = ax .transScale .transform ((event .xdata , event .ydata ))
2605
+
2606
+ 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
2611
+
2612
+ inv_scale = ax .transScale .inverted ()
2613
+ (new_xmin , new_ymin ), (new_xmax , new_ymax ) = inv_scale .transform (
2614
+ [(new_xmin , new_ymin ), (new_xmax , new_ymax )])
2615
+
2616
+ ax .set_xlim (new_xmin , new_xmax )
2617
+ ax .set_ylim (new_ymin , new_ymax )
2618
+
2619
+ ax .figure .canvas .draw_idle ()
2620
+
2621
+
2577
2622
class NonGuiException (Exception ):
2578
2623
"""Raised when trying show a figure in a non-GUI backend."""
2579
2624
pass
@@ -2653,11 +2698,14 @@ def __init__(self, canvas, num):
2653
2698
2654
2699
self .key_press_handler_id = None
2655
2700
self .button_press_handler_id = None
2701
+ self .scroll_handler_id = None
2656
2702
if rcParams ['toolbar' ] != 'toolmanager' :
2657
2703
self .key_press_handler_id = self .canvas .mpl_connect (
2658
2704
'key_press_event' , key_press_handler )
2659
2705
self .button_press_handler_id = self .canvas .mpl_connect (
2660
2706
'button_press_event' , button_press_handler )
2707
+ self .scroll_handler_id = self .canvas .mpl_connect (
2708
+ 'scroll_event' , scroll_handler )
2661
2709
2662
2710
self .toolmanager = (ToolManager (canvas .figure )
2663
2711
if mpl .rcParams ['toolbar' ] == 'toolmanager'
0 commit comments