8000 Move widget functions into matplotlib.testing.widgets. by SidharthBansal · Pull Request #17030 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Move widget functions into matplotlib.testing.widgets. #17030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions lib/matplotlib/testing/widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
========================
Widget testing utilities
========================
Functions that are useful for testing widgets.
See also matplotlib.tests.test_widgets
"""
import matplotlib.pyplot as plt
from unittest import mock


def get_ax():
"""Creates plot and returns its axes"""
fig, ax = plt.subplots(1, 1)
ax.plot([0, 200], [0, 200])
ax.set_aspect(1.0)
ax.figure.canvas.draw()
return ax


def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
"""
Trigger an event

Parameters
----------
tool : matplotlib.widgets.RectangleSelector
etype
the event to trigger
xdata : int
x coord of mouse in data coords
ydata : int
y coord of mouse in data coords
button : int or str
button pressed None, 1, 2, 3, 'up', 'down' (up and down are used
for scroll events)
key
the key depressed when the mouse event triggered (see
:class:`KeyEvent`)
step : int
number of scroll steps (positive for 'up', negative for 'down')
"""
event = mock.Mock()
event.button = button
ax = tool.ax
event.x, event.y = ax.transData.transform([(xdata, ydata),
(xdata, ydata)])[0]
event.xdata, event.ydata = xdata, ydata
event.inaxes = ax
event.canvas = ax.figure.canvas
event.key = key
event.step = step
event.guiEvent = None
event.name = 'Custom'

func = getattr(tool, etype)
func(event)
65 changes: 1 addition & 64 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,13 @@
from types import SimpleNamespace

import matplotlib.widgets as widgets
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import image_comparison
from matplotlib.testing.widgets import do_event, get_ax

from numpy.testing import assert_allclose

import pytest


def get_ax():
fig, ax = plt.subplots(1, 1)
ax.plot([0, 200], [0, 200])
ax.set_aspect(1.0)
ax.figure.canvas.draw()
return ax


def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
"""
*name*
the event name

*canvas*
the FigureCanvas instance generating the event

*guiEvent*
the GUI event that triggered the matplotlib event

*x*
x position - pixels from left of canvas

*y*
y position - pixels from bottom of canvas

*inaxes*
the :class:`~matplotlib.axes.Axes` instance if mouse is over axes

*xdata*
x coord of mouse in data coords

*ydata*
y coord of mouse in data coords

*button*
button pressed None, 1, 2, 3, 'up', 'down' (up and down are used
for scroll events)

*key*
the key depressed when the mouse event triggered (see
:class:`KeyEvent`)

*step*
number of scroll steps (positive for 'up', negative for 'down')
"""
event = SimpleNamespace()
event.button = button
67B9 ax = tool.ax
event.x, event.y = ax.transData.transform([(xdata, ydata),
(xdata, ydata)])[00]
event.xdata, event.ydata = xdata, ydata
event.inaxes = ax
event.canvas = ax.figure.canvas
event.key = key
event.step = step
event.guiEvent = None
event.name = 'Custom'

func = getattr(tool, etype)
func(event)


def check_rectangle(**kwargs):
ax = get_ax()

Expand Down
0