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

Skip to content

Commit 887eb62

Browse files
committed
TST: Add some tests for QuadMesh contains function
* Update QuadMesh.get_cursor_data to handle multiple contains hits * Test that an empty array doesn't return any cursor_data * Test a few points in a standard QuadMesh * Test points within and around a concave QuadMesh
1 parent 2aa71eb commit 887eb62

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

lib/matplotlib/collections.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,12 +2197,6 @@ def draw(self, renderer):
21972197

21982198
def get_cursor_data(self, event):
21992199
contained, info = self.contains(event)
2200-
if len(info["ind"]) == 1:
2201-
ind, = info["ind"]
2202-
array = self.get_array()
2203-
if array is not None:
2204-
return array[ind]
2205-
else:
2206-
return None
2207-
else:
2208-
return None
2200+
if contained and self.get_array() is not None:
2201+
return self.get_array().ravel()[info["ind"]]
2202+
return None

lib/matplotlib/tests/test_collections.py

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import matplotlib.path as mpath
1313
import matplotlib.transforms as mtransforms
1414
from matplotlib.collections import (Collection, LineCollection,
15-
EventCollection, PolyCollection)
15+
EventCollection, PolyCollection,
16+
QuadMesh)
1617
from matplotlib.testing.decorators import check_figures_equal, image_comparison
1718
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
1819

@@ -483,6 +484,81 @@ def test_picking():
483484
assert_array_equal(indices['ind'], [0])
484485

485486

487+
def test_quadmesh_contains():
488+
x = np.arange(4)
489+
X = x[:, None] * x[None, :]
490+
491+
fig, ax = plt.subplots()
492+
mesh = ax.pcolormesh(X)
493+
fig.draw_without_rendering()
494+
xdata, ydata = 0.5, 0.5
495+
x, y = mesh.get_transform().transform((xdata, ydata))
496+
mouse_event = SimpleNamespace(xdata=xdata, ydata=ydata, x=x, y=y)
497+
found, indices = mesh.contains(mouse_event)
498+
assert found
499+
assert_array_equal(indices['ind'], [0])
500+
501+
xdata, ydata = 1.5, 1.5
502+
x, y = mesh.get_transform().transform((xdata, ydata))
503+
mouse_event = SimpleNamespace(xdata=xdata, ydata=ydata, x=x, y=y)
504+
found, indices = mesh.contains(mouse_event)
505+
assert found
506+
assert_array_equal(indices['ind'], [5])
507+
508+
509+
def test_quadmesh_contains_concave():
510+
# Test a concave polygon, V-like shape
511+
x = [[0, -1], [1, 0]]
512+
y = [[0, 1], [1, -1]]
513+
fig, ax = plt.subplots()
514+
mesh = ax.pcolormesh(x, y, [[0]])
515+
fig.draw_without_rendering()
516+
# xdata, ydata, expected
517+
points = [(-0.5, 0.25, True), # left wing
518+
(0, 0.25, False), # between the two wings
519+
(0.5, 0.25, True), # right wing
520+
(0, -0.25, True), # main body
521+
]
522+
for point in points:
523+
xdata, ydata, expected = point
524+
x, y = mesh.get_transform().transform((xdata, ydata))
525+
mouse_event = SimpleNamespace(xdata=xdata, ydata=ydata, x=x, y=y)
526+
found, indices = mesh.contains(mouse_event)
527+
assert found is expected
528+
529+
530+
def test_quadmesh_cursor_data():
531+
x = np.arange(4)
532+
X = x[:, None] * x[None, :]
533+
534+
fig, ax = plt.subplots()
535+
mesh = ax.pcolormesh(X)
536+
# Empty array data
537+
mesh._A = None
538+
fig.draw_without_rendering()
539+
xdata, ydata = 0.5, 0.5
540+
x, y = mesh.get_transform().transform((xdata, ydata))
541+
mouse_event = SimpleNamespace(xdata=xdata, ydata=ydata, x=x, y=y)
542+
# Empty collection should return None
543+
assert mesh.get_cursor_data(mouse_event) is None
544+
545+
# Now test adding the array data, to make sure we do get a value
546+
mesh.set_array(np.ones((X.shape)))
547+
assert_array_equal(mesh.get_cursor_data(mouse_event), [1])
548+
549+
550+
def test_quadmesh_cursor_data_multiple_points():
551+
x = [1, 2, 1, 2]
552+
fig, ax = plt.subplots()
553+
mesh = ax.pcolormesh(x, x, np.ones((3, 3)))
554+
fig.draw_without_rendering()
555+
xdata, ydata = 1.5, 1.5
556+
x, y = mesh.get_transform().transform((xdata, ydata))
557+
mouse_event = SimpleNamespace(xdata=xdata, ydata=ydata, x=x, y=y)
558+
# All quads are covering the same square
559+
assert_array_equal(mesh.get_cursor_data(mouse_event), np.ones(9))
560+
561+
486562
def test_linestyle_single_dashes():
487563
plt.scatter([0, 1, 2], [0, 1, 2], linestyle=(0., [2., 2.]))
488564
plt.draw()
@@ -749,8 +825,6 @@ def test_quadmesh_deprecated_signature(
749825
fig_test, fig_ref, flat_ref, kwargs):
750826
# test that the new and old quadmesh signature produce the same results
751827
# remove when the old QuadMesh.__init__ signature expires (v3.5+2)
752-
from matplotlib.collections import QuadMesh
753-
754828
x = [0, 1, 2, 3.]
755829
y = [1, 2, 3.]
756830
X, Y = np.meshgrid(x, y)

0 commit comments

Comments
 (0)