Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ada649

Browse files
Move widget
1 parent 53faed9 commit 5ada649

File tree

2 files changed

+57
-64
lines changed

2 files changed

+57
-64
lines changed

‎lib/matplotlib/testing/widgets.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
def get_ax():
12+
""" Creates plot and returns its axes"""
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+
Trigger an event
23+
24+
Parameters
25+
----------
26+
tool : a matplotlib.widgets.RectangleSelector instance
27+
etype
28+
the event to trigger
29+
xdata : int
30+
x coord of mouse in data coords
31+
ydata : int
32+
y coord of mouse in data coords
33+
button : int or str
34+
button pressed None, 1, 2, 3, 'up', 'down' (up and down are used
35+
for scroll events)
36+
key
37+
the key depressed when the mouse event triggered (see
38+
:class:`KeyEvent`)
39+
step : int
40+
number of scroll steps (positive for 'up', negative for 'down')
41+
"""
42+
event = mock.Mock()
43+
event.button = button
44+
ax = tool.ax
45+
event.x, event.y = ax.transData.transform([(xdata, ydata),
46+
(xdata, ydata)])[00]
47+
event.xdata, event.ydata = xdata, ydata
48+
event.inaxes = ax
49+
event.canvas = ax.figure.canvas
50+
event.key = key
51+
event.step = step
52+
event.guiEvent = None
53+
event.name = 'Custom'
54+
55+
func = getattr(tool, etype)
56+
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)

AltStyle によって変換されたページ (->オリジナル) /