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

Skip to content

Commit 2f6a7a4

Browse files
authored
Merge pull request #16757 from anntzer/zoom
Simplify interactive zoom handling.
2 parents 32f08df + 27169ea commit 2f6a7a4

File tree

3 files changed

+37
-57
lines changed

3 files changed

+37
-57
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,11 +2697,9 @@ def __init__(self, canvas):
26972697
# This cursor will be set after the initial draw.
26982698
self._lastCursor = cursors.POINTER
26992699
self._init_toolbar()
2700-
self._idDrag = self.canvas.mpl_connect(
2700+
self._id_drag = self.canvas.mpl_connect(
27012701
'motion_notify_event', self.mouse_move)
2702-
2703-
self._ids_zoom = []
2704-
self._zoom_mode = None
2702+
self._id_zoom = None
27052703

27062704
self._button_pressed = None # determined by button pressed at start
27072705

@@ -2904,23 +2902,22 @@ def press_pan(self, event):
29042902
a.get_navigate() and a.can_pan()):
29052903
a.start_pan(x, y, event.button)
29062904
self._xypress.append((a, i))
2907-
self.canvas.mpl_disconnect(self._idDrag)
2908-
self._idDrag = self.canvas.mpl_connect('motion_notify_event',
2909-
self.drag_pan)
2905+
self.canvas.mpl_disconnect(self._id_drag)
2906+
self._id_drag = self.canvas.mpl_connect(
2907+
'motion_notify_event', self.drag_pan)
29102908
self.press(event)
29112909

29122910
def press_zoom(self, event):
29132911
"""Callback for mouse button press in zoom to rect mode."""
29142912
# If we're already in the middle of a zoom, pressing another
29152913
# button works to "cancel"
2916-
if self._ids_zoom:
2917-
for zoom_id in self._ids_zoom:
2918-
self.canvas.mpl_disconnect(zoom_id)
2914+
if self._id_zoom is not None:
2915+
self.canvas.mpl_disconnect(self._id_zoom)
29192916
self.release(event)
29202917
self.draw()
29212918
self._xypress = None
29222919
self._button_pressed = None
2923-
self._ids_zoom = []
2920+
self._id_zoom = None
29242921
return
29252922

29262923
if event.button in [1, 3]:
@@ -2935,30 +2932,16 @@ def press_zoom(self, event):
29352932

29362933
x, y = event.x, event.y
29372934
self._xypress = []
2938-
for i, a in enumerate(self.canvas.figure.get_axes()):
2935+
for a in self.canvas.figure.get_axes():
29392936
if (x is not None and y is not None and a.in_axes(event) and
29402937
a.get_navigate() and a.can_zoom()):
2941-
self._xypress.append((x, y, a, i, a._get_view()))
2942-
2943-
id1 = self.canvas.mpl_connect('motion_notify_event', self.drag_zoom)
2944-
id2 = self.canvas.mpl_connect('key_press_event',
2945-
self._switch_on_zoom_mode)
2946-
id3 = self.canvas.mpl_connect('key_release_event',
2947-
self._switch_off_zoom_mode)
2938+
self._xypress.append((x, y, a))
29482939

2949-
self._ids_zoom = id1, id2, id3
2950-
self._zoom_mode = event.key
2940+
self._id_zoom = self.canvas.mpl_connect(
2941+
'motion_notify_event', self.drag_zoom)
29512942

29522943
self.press(event)
29532944

2954-
def _switch_on_zoom_mode(self, event):
2955-
self._zoom_mode = event.key
2956-
self.mouse_move(event)
2957-
2958-
def _switch_off_zoom_mode(self, event):
2959-
self._zoom_mode = None
2960-
self.mouse_move(event)
2961-
29622945
def push_current(self):
29632946
"""Push the current view limits and position onto the stack."""
29642947
self._nav_stack.push(
@@ -2978,8 +2961,8 @@ def release_pan(self, event):
29782961

29792962
if self._button_pressed is None:
29802963
return
2981-
self.canvas.mpl_disconnect(self._idDrag)
2982-
self._idDrag = self.canvas.mpl_connect(
2964+
self.canvas.mpl_disconnect(self._id_drag)
2965+
self._id_drag = self.canvas.mpl_connect(
29832966
'motion_notify_event', self.mouse_move)
29842967
for a, ind in self._xypress:
29852968
a.end_pan()
@@ -3003,20 +2986,20 @@ def drag_zoom(self, event):
30032986
"""Callback for dragging in zoom mode."""
30042987
if self._xypress:
30052988
x, y = event.x, event.y
3006-
lastx, lasty, a, ind, view = self._xypress[0]
2989+
lastx, lasty, a = self._xypress[0]
30072990
(x1, y1), (x2, y2) = np.clip(
30082991
[[lastx, lasty], [x, y]], a.bbox.min, a.bbox.max)
3009-
if self._zoom_mode == "x":
2992+
if event.key == "x":
30102993
y1, y2 = a.bbox.intervaly
3011-
elif self._zoom_mode == "y":
2994+
elif event.key == "y":
30122995
x1, x2 = a.bbox.intervalx
30132996
self.draw_rubberband(event, x1, y1, x2, y2)
30142997

30152998
def release_zoom(self, event):
30162999
"""Callback for mouse button release in zoom to rect mode."""
3017-
for zoom_id in self._ids_zoom:
3018-
self.canvas.mpl_disconnect(zoom_id)
3019-
self._ids_zoom = []
3000+
if self._id_zoom is not None:
3001+
self.canvas.mpl_disconnect(self._id_zoom)
3002+
self._id_zoom = None
30203003

30213004
self.remove_rubberband()
30223005

@@ -3025,14 +3008,13 @@ def release_zoom(self, event):
30253008

30263009
last_a = []
30273010

3028-
for cur_xypress in self._xypress:
3011+
for lastx, lasty, a in self._xypress:
30293012
x, y = event.x, event.y
3030-
lastx, lasty, a, ind, view = cur_xypress
30313013
# ignore singular clicks - 5 pixels is a threshold
30323014
# allows the user to "cancel" a zoom action
30333015
# by zooming by less than 5 pixels
3034-
if ((abs(x - lastx) < 5 and self._zoom_mode != "y") or
3035-
(abs(y - lasty) < 5 and self._zoom_mode != "x")):
3016+
if ((abs(x - lastx) < 5 and event.key != "y") or
3017+
(abs(y - lasty) < 5 and event.key != "x")):
30363018
self._xypress = None
30373019
self.release(event)
30383020
self.draw()
@@ -3056,14 +3038,12 @@ def release_zoom(self, event):
30563038
continue
30573039

30583040
a._set_view_from_bbox((lastx, lasty, x, y), direction,
3059-
self._zoom_mode, twinx, twiny)
3041+
event.key, twinx, twiny)
30603042

30613043
self.draw()
30623044
self._xypress = None
30633045
self._button_pressed = None
30643046

3065-
self._zoom_mode = None
3066-
30673047
self.push_current()
30683048
self.release(event)
30693049

lib/matplotlib/backend_tools.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class SetCursorBase(ToolBase):
253253
"""
254254
def __init__(self, *args, **kwargs):
255255
ToolBase.__init__(self, *args, **kwargs)
256-
self._idDrag = None
256+
self._id_drag = None
257257
self._cursor = None
258258
self._default_cursor = cursors.POINTER
259259
self._last_cursor = self._default_cursor
@@ -265,11 +265,11 @@ def __init__(self, *args, **kwargs):
265265
self._add_tool(tool)
266266

267267
def set_figure(self, figure):
268-
if self._idDrag:
269-
self.canvas.mpl_disconnect(self._idDrag)
268+
if self._id_drag:
269+
self.canvas.mpl_disconnect(self._id_drag)
270270
ToolBase.set_figure(self, figure)
271271
if figure:
272-
self._idDrag = self.canvas.mpl_connect(
272+
self._id_drag = self.canvas.mpl_connect(
273273
'motion_notify_event', self._set_cursor_cbk)
274274

275275
def _tool_trigger_cbk(self, event):
@@ -322,15 +322,15 @@ class ToolCursorPosition(ToolBase):
322322
This tool runs in the background reporting the position of the cursor.
323323
"""
324324
def __init__(self, *args, **kwargs):
325-
self._idDrag = None
325+
self._id_drag = None
326326
ToolBase.__init__(self, *args, **kwargs)
327327

328328
def set_figure(self, figure):
329-
if self._idDrag:
330-
self.canvas.mpl_disconnect(self._idDrag)
329+
if self._id_drag:
330+
self.canvas.mpl_disconnect(self._id_drag)
331331
ToolBase.set_figure(self, figure)
332332
if figure:
333-
self._idDrag = self.canvas.mpl_connect(
333+
self._id_drag = self.canvas.mpl_connect(
334334
'motion_notify_event', self.send_message)
335335

336336
def send_message(self, event):
@@ -972,12 +972,12 @@ class ToolPan(ZoomPanBase):
972972

973973
def __init__(self, *args):
974974
ZoomPanBase.__init__(self, *args)
975-
self._idDrag = None
975+
self._id_drag = None
976976

977977
def _cancel_action(self):
978978
self._button_pressed = None
979979
self._xypress = []
980-
self.figure.canvas.mpl_disconnect(self._idDrag)
980+
self.figure.canvas.mpl_disconnect(self._id_drag)
981981
self.toolmanager.messagelock.release(self)
982982
self.toolmanager.get_tool(_views_positions).refresh_locators()
983983

@@ -999,15 +999,15 @@ def _press(self, event):
999999
a.start_pan(x, y, event.button)
10001000
self._xypress.append((a, i))
10011001
self.toolmanager.messagelock(self)
1002-
self._idDrag = self.figure.canvas.mpl_connect(
1002+
self._id_drag = self.figure.canvas.mpl_connect(
10031003
'motion_notify_event', self._mouse_move)
10041004

10051005
def _release(self, event):
10061006
if self._button_pressed is None:
10071007
self._cancel_action()
10081008
return
10091009

1010-
self.figure.canvas.mpl_disconnect(self._idDrag)
1010+
self.figure.canvas.mpl_disconnect(self._id_drag)
10111011
self.toolmanager.messagelock.release(self)
10121012

10131013
for a, _ind in self._xypress:

lib/matplotlib/backends/backend_wx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ def GetToolBar(self):
10171017

10181018
def Destroy(self, *args, **kwargs):
10191019
try:
1020-
self.canvas.mpl_disconnect(self.toolbar._idDrag)
1020+
self.canvas.mpl_disconnect(self.toolbar._id_drag)
10211021
# Rationale for line above: see issue 2941338.
10221022
except AttributeError:
10231023
pass # classic toolbar lacks the attribute

0 commit comments

Comments
 (0)