diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 54728fe05d6b..e0ea6cea5de5 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1365,8 +1365,10 @@ def __init__(self, name, canvas, x, y, guiEvent=None): *x*, *y* in figure coords, 0,0 = bottom, left """ Event.__init__(self, name, canvas, guiEvent=guiEvent) - self.x = x # x position - pixels from left of canvas - self.y = y # y position - pixels from right of canvas + # x position - pixels from left of canvas + self.x = int(x) if x is not None else x + # y position - pixels from right of canvas + self.y = int(y) if y is not None else y self.inaxes = None # the Axes instance if mouse us over axes self.xdata = None # x coord of mouse in data coords self.ydata = None # y coord of mouse in data coords diff --git a/lib/matplotlib/tests/test_backend_bases.py b/lib/matplotlib/tests/test_backend_bases.py index cd8db6bc08ef..048da404c81f 100644 --- a/lib/matplotlib/tests/test_backend_bases.py +++ b/lib/matplotlib/tests/test_backend_bases.py @@ -1,5 +1,6 @@ from matplotlib.backend_bases import FigureCanvasBase from matplotlib.backend_bases import RendererBase +from matplotlib.backend_bases import LocationEvent import matplotlib.pyplot as plt import matplotlib.transforms as transforms @@ -77,3 +78,24 @@ def test_non_gui_warning(): assert len(rec) == 1 assert ('Matplotlib is currently using pdf, which is a non-GUI backend' in str(rec[0].message)) + + +def test_location_event_position(): + # LocationEvent should cast its x and y arguments + # to int unless it is None + fig = plt.figure() + canvas = FigureCanvasBase(fig) + test_positions = [(42, 24), (None, 42), (None, None), + (200, 100.01), (205.75, 2.0)] + for x, y in test_positions: + event = LocationEvent("test_event", canvas, x, y) + if x is None: + assert event.x is None + else: + assert event.x == int(x) + assert isinstance(event.x, int) + if y is None: + assert event.y is None + else: + assert event.y == int(y) + assert isinstance(event.y, int)