|
13 | 13 | from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
|
14 | 14 | from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
|
15 | 15 |
|
| 16 | +from matplotlib.backend_bases import MouseEvent |
16 | 17 | from matplotlib.colors import LogNorm
|
17 | 18 | from itertools import product
|
18 | 19 |
|
@@ -171,3 +172,64 @@ def test_zooming_with_inverted_axes():
|
171 | 172 | ax.axis([3, 1, 3, 1])
|
172 | 173 | inset_ax = zoomed_inset_axes(ax, zoom=2.5, loc=4)
|
173 | 174 | inset_ax.axis([1.4, 1.1, 1.4, 1.1])
|
| 175 | + |
| 176 | +class TestPickingCallbacksOverlap(object): |
| 177 | + """Test pick events on normal, host or parasite axes.""" |
| 178 | + # Two rectangles are drawn and "clicked on", a small one and a big one |
| 179 | + # enclosing the small one. The axis on which they are drawn as well as the |
| 180 | + # rectangle that is clicked on are varied. |
| 181 | + # In each case we expect that both rectangles are picked if we click on the |
| 182 | + # small one and only the big one is picked if we click on the big one. |
| 183 | + # Also tests picking on normal axes ("gca") as a control. |
| 184 | + def setUp(self): |
| 185 | + self.q = six.Queue() |
| 186 | + self.big = plt.Rectangle((0.25, 0.25), 0.5, 0.5, picker=5) |
| 187 | + self.small = plt.Rectangle((0.4, 0.4), 0.2, 0.2, facecolor="r", |
| 188 | + picker=5) |
| 189 | + plt.gcf().canvas.mpl_connect('pick_event', self.on_pick) |
| 190 | + def on_pick(self, event): |
| 191 | + self.q.put(event) |
| 192 | + def test_picking_simple(self): |
| 193 | + # Configurations here are of the form: ( big_on_axes, small_on_axes ) |
| 194 | + for rectangles_on_axes in [ ("gca", "gca"), ("host", "host"), |
| 195 | + ("host", "parasite"), ("parasite", "host"), |
| 196 | + ("parasite", "parasite") ]: |
| 197 | + for click_on in [ "big", "small" ]: |
| 198 | + yield self.run, rectangles_on_axes, click_on |
| 199 | + def run(self, rectangles_on_axes, click_on): |
| 200 | + # Axes setup |
| 201 | + axes = { "gca": None, "host": None, "parasite": None } |
| 202 | + if "gca" in rectangles_on_axes: |
| 203 | + axes["gca"] = plt.gca() |
| 204 | + if "host" in rectangles_on_axes or "parasite" in rectangles_on_axes: |
| 205 | + axes["host"] = host_subplot(111) |
| 206 | + axes["parasite"] = axes["host"].twin() |
| 207 | + # Add rectangles to axes |
| 208 | + (big_on_axes, small_on_axes) = rectangles_on_axes |
| 209 | + axes[big_on_axes].add_patch(self.big) |
| 210 | + axes[small_on_axes].add_patch(self.small) |
| 211 | + # Simulate picking with click mouse event |
| 212 | + if click_on == "big": |
| 213 | + click_axes = axes[big_on_axes] |
| 214 | + axes_coords = (0.3, 0.3) |
| 215 | + else: |
| 216 | + click_axes = axes[small_on_axes] |
| 217 | + axes_coords = (0.5, 0.5) |
| 218 | + # In reality, mouse events never happen on parasite axes, only host axes |
| 219 | + if click_axes is axes["parasite"]: |
| 220 | + click_axes = axes["host"] |
| 221 | + (x, y) = click_axes.transAxes.transform(axes_coords) |
| 222 | + m = MouseEvent("button_press_event", click_axes.figure.canvas, x, y, |
| 223 | + button=1) |
| 224 | + click_axes.pick(m) |
| 225 | + # Wait at most a second for events; actual waiting only happens if |
| 226 | + # something is wrong and tests fail, this won't slow down normal testing |
| 227 | + n_events = 2 if click_on == "small" else 1 |
| 228 | + event_rects = [] |
| 229 | + for i in range(n_events): |
| 230 | + event = self.q.get(True, 0.5) |
| 231 | + event_rects.append(event.artist) |
| 232 | + assert self.big in event_rects |
| 233 | + if click_on == "small": |
| 234 | + assert self.small in event_rects |
| 235 | + |
0 commit comments