|
12 | 12 | import matplotlib.path as mpath
|
13 | 13 | import matplotlib.transforms as mtransforms
|
14 | 14 | from matplotlib.collections import (Collection, LineCollection,
|
15 |
| - EventCollection, PolyCollection) |
| 15 | + EventCollection, PolyCollection, |
| 16 | + QuadMesh) |
16 | 17 | from matplotlib.testing.decorators import check_figures_equal, image_comparison
|
17 | 18 | from matplotlib._api.deprecation import MatplotlibDeprecationWarning
|
18 | 19 |
|
@@ -483,6 +484,81 @@ def test_picking():
|
483 | 484 | assert_array_equal(indices['ind'], [0])
|
484 | 485 |
|
485 | 486 |
|
| 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 | + |
486 | 562 | def test_linestyle_single_dashes():
|
487 | 563 | plt.scatter([0, 1, 2], [0, 1, 2], linestyle=(0., [2., 2.]))
|
488 | 564 | plt.draw()
|
@@ -749,8 +825,6 @@ def test_quadmesh_deprecated_signature(
|
749 | 825 | fig_test, fig_ref, flat_ref, kwargs):
|
750 | 826 | # test that the new and old quadmesh signature produce the same results
|
751 | 827 | # remove when the old QuadMesh.__init__ signature expires (v3.5+2)
|
752 |
| - from matplotlib.collections import QuadMesh |
753 |
| - |
754 | 828 | x = [0, 1, 2, 3.]
|
755 | 829 | y = [1, 2, 3.]
|
756 | 830 | X, Y = np.meshgrid(x, y)
|
|
0 commit comments