From d3750f751c8b8ca133327dcd1c654147650b587f Mon Sep 17 00:00:00 2001 From: SidharthBansal Date: Fri, 17 Apr 2020 01:02:45 +0530 Subject: [PATCH 1/3] Move widgets --- lib/matplotlib/testing/widgets.py | 57 ++++++++++++++++++++++++ lib/matplotlib/tests/test_widgets.py | 65 +--------------------------- 2 files changed, 58 insertions(+), 64 deletions(-) create mode 100644 lib/matplotlib/testing/widgets.py diff --git a/lib/matplotlib/testing/widgets.py b/lib/matplotlib/testing/widgets.py new file mode 100644 index 000000000000..566cd17e5ea3 --- /dev/null +++ b/lib/matplotlib/testing/widgets.py @@ -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 : a matplotlib.widgets.RectangleSelector instance + 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)])[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) diff --git a/lib/matplotlib/tests/test_widgets.py b/lib/matplotlib/tests/test_widgets.py index 0d50389ae35f..d7c8f0d2f48c 100644 --- a/lib/matplotlib/tests/test_widgets.py +++ b/lib/matplotlib/tests/test_widgets.py @@ -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 - 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() From e83e0b4c4a4b65e400c269dbb59f896061c88aa3 Mon Sep 17 00:00:00 2001 From: Sidharth Bansal <20972099+SidharthBansal@users.noreply.github.com> Date: Tue, 21 Apr 2020 19:38:48 +0530 Subject: [PATCH 2/3] Update lib/matplotlib/testing/widgets.py Co-Authored-By: Elliott Sales de Andrade --- lib/matplotlib/testing/widgets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/testing/widgets.py b/lib/matplotlib/testing/widgets.py index 566cd17e5ea3..42515cd80c9b 100644 --- a/lib/matplotlib/testing/widgets.py +++ b/lib/matplotlib/testing/widgets.py @@ -24,7 +24,7 @@ def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1): Parameters ---------- - tool : a matplotlib.widgets.RectangleSelector instance + tool : matplotlib.widgets.RectangleSelector etype the event to trigger xdata : int From e34f406e2aee13e7e4d90084d826eef50c3aafe4 Mon Sep 17 00:00:00 2001 From: Sidharth Bansal <20972099+SidharthBansal@users.noreply.github.com> Date: Tue, 21 Apr 2020 19:38:59 +0530 Subject: [PATCH 3/3] Update lib/matplotlib/testing/widgets.py Co-Authored-By: Elliott Sales de Andrade --- lib/matplotlib/testing/widgets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/testing/widgets.py b/lib/matplotlib/testing/widgets.py index 42515cd80c9b..7c77e68327a1 100644 --- a/lib/matplotlib/testing/widgets.py +++ b/lib/matplotlib/testing/widgets.py @@ -44,7 +44,7 @@ def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1): event.button = button ax = tool.ax event.x, event.y = ax.transData.transform([(xdata, ydata), - (xdata, ydata)])[00] + (xdata, ydata)])[0] event.xdata, event.ydata = xdata, ydata event.inaxes = ax event.canvas = ax.figure.canvas