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

Skip to content

Commit 5e9ec0c

Browse files
Move widget functions into matplotlib.testing.widgets
1 parent 2227aa3 commit 5e9ec0c

File tree

2 files changed

+60
-64
lines changed

2 files changed

+60
-64
lines changed

lib/matplotlib/testing/widgets.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
========================
3+
Widget testing utilities
4+
========================
5+
Functions that are useful for testing widgets.
6+
See also matplotlib.tests.test_widgets
7+
"""
8+
9+
import matplotlib.pyplot as plt
10+
11+
try:
12+
# mock in python 3.3+
13+
from unittest import mock
14+
except ImportError:
15+
import mock
16+
17+
18+
def get_ax():
19+
fig, ax = plt.subplots(1, 1)
20+
ax.plot([0, 200], [0, 200])
21+
ax.set_aspect(1.0)
22+
ax.figure.canvas.draw()
23+
return ax
24+
25+
26+
def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
27+
"""
28+
*tool*
29+
a matplotlib.widgets.RectangleSelector instance
30+
*etype*
31+
the event to trigger
32+
*xdata*
33+
x coord of mouse in data coords
34+
*ydata*
35+
y coord of mouse in data coords
36+
*button*
37+
button pressed None, 1, 2, 3, 'up', 'down' (up and down are used
38+
for scroll events)
39+
*key*
40+
the key depressed when the mouse event triggered (see
41+
:class:`KeyEvent`)
42+
*step*
43+
number of scroll steps (positive for 'up', negative for 'down')
44+
"""
45+
event = mock.Mock()
46+
event.button = button
47+
ax = tool.ax
48+
event.x, event.y = ax.transData.transform([(xdata, ydata),
49+
(xdata, ydata)])[00]
50+
event.xdata, event.ydata = xdata, ydata
51+
event.inaxes = ax
52+
event.canvas = ax.figure.canvas
53+
event.key = key
54+
event.step = step
55+
event.guiEvent = None
56+
event.name = 'Custom'
57+
58+
func = getattr(tool, etype)
59+
func(event)

lib/matplotlib/tests/test_widgets.py

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,13 @@
1-
from types import SimpleNamespace
2-
31
import matplotlib.widgets as widgets
42
import matplotlib.pyplot as plt
53
from matplotlib.testing.decorators import image_comparison
4+
from matplotlib.testing.widgets import do_event, get_ax
65

76
from numpy.testing import assert_allclose
87

98
import pytest
109

1110

12-
def get_ax():
13-
fig, ax = plt.subplots(1, 1)
14-
ax.plot([0, 200], [0, 200])
15-
ax.set_aspect(1.0)
16-
ax.figure.canvas.draw()
17-
return ax
18-
19-
20-
def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
21-
"""
22-
*name*
23-
the event name
24-
25-
*canvas*
26-
the FigureCanvas instance generating the event
27-
28-
*guiEvent*
29-
the GUI event that triggered the matplotlib event
30-
31-
*x*
32-
x position - pixels from left of canvas
33-
34-
*y*
35-
y position - pixels from bottom of canvas
36-
37-
*inaxes*
38-
the :class:`~matplotlib.axes.Axes` instance if mouse is over axes
39-
40-
*xdata*
41-
x coord of mouse in data coords
42-
43-
*ydata*
44-
y coord of mouse in data coords
45-
46-
*button*
47-
button pressed None, 1, 2, 3, 'up', 'down' (up and down are used
48-
for scroll events)
49-
50-
*key*
51-
the key depressed when the mouse event triggered (see
52-
:class:`KeyEvent`)
53-
54-
*step*
55-
number of scroll steps (positive for 'up', negative for 'down')
56-
"""
57-
event = SimpleNamespace()
58-
event.button = button
59-
ax = tool.ax
60-
event.x, event.y = ax.transData.transform([(xdata, ydata),
61-
(xdata, ydata)])[00]
62-
event.xdata, event.ydata = xdata, ydata
63-
event.inaxes = ax
64-
event.canvas = ax.figure.canvas
65-
event.key = key
66-
event.step = step
67-
event.guiEvent = None
68-
event.name = 'Custom'
69-
70-
func = getattr(tool, etype)
71-
func(event)
72-
73-
7411
def check_rectangle(**kwargs):
7512
ax = get_ax()
7613

0 commit comments

Comments
 (0)