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

Skip to content

Commit d3750f7

Browse files
Move widgets
1 parent 53faed9 commit d3750f7

File tree

2 files changed

+58
-64
lines changed

2 files changed

+58
-64
lines changed

lib/matplotlib/testing/widgets.py

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