|
5 | 5 |
|
6 | 6 | import matplotlib
|
7 | 7 | import matplotlib.pyplot as plt
|
8 |
| -from matplotlib.testing.decorators import image_comparison |
| 8 | +from matplotlib.backend_bases import MouseEvent |
| 9 | +from matplotlib.externals.six.moves.queue import Queue |
| 10 | +from matplotlib.testing.decorators import image_comparison, cleanup |
9 | 11 | from mpl_toolkits.axes_grid1 import make_axes_locatable, host_subplot
|
10 | 12 | from itertools import product
|
11 | 13 | import numpy as np
|
@@ -82,6 +84,69 @@ def test_twin_axes_empty_and_removed():
|
82 | 84 | horizontalalignment="center", verticalalignment="center")
|
83 | 85 | plt.subplots_adjust(wspace=0.5, hspace=1)
|
84 | 86 |
|
| 87 | + |
| 88 | +class TestPickingCallbacksOverlap(object): |
| 89 | + """Test pick events on normal, host or parasite axes.""" |
| 90 | + # Two rectangles are drawn and "clicked on", a small one and a big one |
| 91 | + # enclosing the small one. The axis on which they are drawn as well as the |
| 92 | + # rectangle that is clicked on are varied. |
| 93 | + # In each case we expect that both rectangles are picked if we click on the |
| 94 | + # small one and only the big one is picked if we click on the big one. |
| 95 | + # Also tests picking on normal axes ("gca") as a control. |
| 96 | + def setUp(self): |
| 97 | + self.q = Queue() |
| 98 | + self.big = plt.Rectangle((0.25, 0.25), 0.5, 0.5, picker=5) |
| 99 | + self.small = plt.Rectangle((0.4, 0.4), 0.2, 0.2, facecolor="r", |
| 100 | + picker=5) |
| 101 | + plt.gcf().canvas.mpl_connect('pick_event', self.on_pick) |
| 102 | + def on_pick(self, event): |
| 103 | + self.q.put(event) |
| 104 | + def test_picking_simple(self): |
| 105 | + # Configurations here are of the form: ( big_on_axes, small_on_axes ) |
| 106 | + for rectangles_on_axes in [ ("gca", "gca"), ("host", "host"), |
| 107 | + ("host", "parasite"), ("parasite", "host"), |
| 108 | + ("parasite", "parasite") ]: |
| 109 | + for click_on in [ "big", "small" ]: |
| 110 | + yield self.run, rectangles_on_axes, click_on |
| 111 | + @cleanup |
| 112 | + def run(self, rectangles_on_axes, click_on): |
| 113 | + # Axes setup |
| 114 | + axes = { "gca": None, "host": None, "parasite": None } |
| 115 | + if "gca" in rectangles_on_axes: |
| 116 | + axes["gca"] = plt.gca() |
| 117 | + if "host" in rectangles_on_axes or "parasite" in rectangles_on_axes: |
| 118 | + axes["host"] = host_subplot(111) |
| 119 | + axes["parasite"] = axes["host"].twin() |
| 120 | + # Add rectangles to axes |
| 121 | + (big_on_axes, small_on_axes) = rectangles_on_axes |
| 122 | + axes[big_on_axes].add_patch(self.big) |
| 123 | + axes[small_on_axes].add_patch(self.small) |
| 124 | + # Simulate picking with click mouse event |
| 125 | + if click_on == "big": |
| 126 | + click_axes = axes[big_on_axes] |
| 127 | + axes_coords = (0.3, 0.3) |
| 128 | + else: |
| 129 | + click_axes = axes[small_on_axes] |
| 130 | + axes_coords = (0.5, 0.5) |
| 131 | + # In reality, mouse events never happen on parasite axes, only host axes |
| 132 | + if click_axes is axes["parasite"]: |
| 133 | + click_axes = axes["host"] |
| 134 | + (x, y) = click_axes.transAxes.transform(axes_coords) |
| 135 | + m = MouseEvent("button_press_event", click_axes.figure.canvas, x, y, |
| 136 | + button=1) |
| 137 | + click_axes.pick(m) |
| 138 | + # Wait at most a second for events; actual waiting only happens if |
| 139 | + # something is wrong and tests fail, this won't slow down normal testing |
| 140 | + n_events = 2 if click_on == "small" else 1 |
| 141 | + event_rects = [] |
| 142 | + for i in range(n_events): |
| 143 | + event = self.q.get(True, 0.5) |
| 144 | + event_rects.append(event.artist) |
| 145 | + assert self.big in event_rects |
| 146 | + if click_on == "small": |
| 147 | + assert self.small in event_rects |
| 148 | + |
| 149 | + |
85 | 150 | if __name__ == '__main__':
|
86 | 151 | import nose
|
87 | 152 | nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
|
0 commit comments