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

Skip to content

Commit 06fd413

Browse files
authored
Merge pull request #11530 from TarasKuzyo/int-loc-event
FIX: Ensuring both x and y attrs of LocationEvent are int
2 parents a53a04c + eae165d commit 06fd413

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,8 +1365,10 @@ def __init__(self, name, canvas, x, y, guiEvent=None):
13651365
*x*, *y* in figure coords, 0,0 = bottom, left
13661366
"""
13671367
Event.__init__(self, name, canvas, guiEvent=guiEvent)
1368-
self.x = x # x position - pixels from left of canvas
1369-
self.y = y # y position - pixels from right of canvas
1368+
# x position - pixels from left of canvas
1369+
self.x = int(x) if x is not None else x
1370+
# y position - pixels from right of canvas
1371+
self.y = int(y) if y is not None else y
13701372
self.inaxes = None # the Axes instance if mouse us over axes
13711373
self.xdata = None # x coord of mouse in data coords
13721374
self.ydata = None # y coord of mouse in data coords

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from matplotlib.backend_bases import FigureCanvasBase
22
from matplotlib.backend_bases import RendererBase
3+
from matplotlib.backend_bases import LocationEvent
34

45
import matplotlib.pyplot as plt
56
import matplotlib.transforms as transforms
@@ -77,3 +78,24 @@ def test_non_gui_warning():
7778
assert len(rec) == 1
7879
assert ('Matplotlib is currently using pdf, which is a non-GUI backend'
7980
in str(rec[0].message))
81+
82+
83+
def test_location_event_position():
84+
# LocationEvent should cast its x and y arguments
85+
# to int unless it is None
86+
fig = plt.figure()
87+
canvas = FigureCanvasBase(fig)
88+
test_positions = [(42, 24), (None, 42), (None, None),
89+
(200, 100.01), (205.75, 2.0)]
90+
for x, y in test_positions:
91+
event = LocationEvent("test_event", canvas, x, y)
92+
if x is None:
93+
assert event.x is None
94+
else:
95+
assert event.x == int(x)
96+
assert isinstance(event.x, int)
97+
if y is None:
98+
assert event.y is None
99+
else:
100+
assert event.y == int(y)
101+
assert isinstance(event.y, int)

0 commit comments

Comments
 (0)