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

Skip to content

Fix issue #7372, move widget functions into matplotlib.testing.widgets. #9905

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions lib/matplotlib/testing/widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
========================
Widget testing utilities
========================

Functions that are useful for testing widgets.
See also matplotlib.tests.test_widgets
"""

import matplotlib.pyplot as plt

try:
# mock in python 3.3+
from unittest import mock
except ImportError:
import mock


def get_ax():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am 50/50 on moving this function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the name of this function does not reflect its body.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you should give it a better name in testing. I assume that functions defined in tests are not part of the public api. So this change would be matplotlib-internal.

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):
"""
*tool*
a matplotlib.widgets.RectangleSelector instance

*etype*
the event to trigger

*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 = mock.Mock()
event.button = button
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)
72 changes: 3 additions & 69 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,15 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

try:
# mock in python 3.3+
from unittest import mock
except ImportError:
import mock

import matplotlib.widgets as widgets
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import image_comparison

from numpy.testing import assert_allclose

import pytest

from matplotlib.testing.decorators import image_comparison
from matplotlib.testing.widgets import do_event, get_ax

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 = mock.Mock()
event.button = button
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)
from numpy.testing import assert_allclose


def check_rectangle(**kwargs):
Expand Down