|
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 |
| 17 | +from matplotlib.externals.six.moves.queue import Queue |
16 | 18 | from matplotlib.colors import LogNorm
|
17 | 19 | from itertools import product
|
18 | 20 |
|
@@ -157,6 +159,68 @@ def get_demo_image():
|
157 | 159 | ax.add_artist(asb)
|
158 | 160 |
|
159 | 161 |
|
| 162 | +class TestPickingCallbacksOverlap(object): |
| 163 | + """Test pick events on normal, host or parasite axes.""" |
| 164 | + # Two rectangles are drawn and "clicked on", a small one and a big one |
| 165 | + # enclosing the small one. The axis on which they are drawn as well as the |
| 166 | + # rectangle that is clicked on are varied. |
| 167 | + # In each case we expect that both rectangles are picked if we click on the |
| 168 | + # small one and only the big one is picked if we click on the big one. |
| 169 | + # Also tests picking on normal axes ("gca") as a control. |
| 170 | + def setUp(self): |
| 171 | + self.q = Queue() |
| 172 | + self.big = plt.Rectangle((0.25, 0.25), 0.5, 0.5, picker=5) |
| 173 | + self.small = plt.Rectangle((0.4, 0.4), 0.2, 0.2, facecolor="r", |
| 174 | + picker=5) |
| 175 | + plt.gcf().canvas.mpl_connect('pick_event', self.on_pick) |
| 176 | + def on_pick(self, event): |
| 177 | + self.q.put(event) |
| 178 | + def test_picking_simple(self): |
| 179 | + # Configurations here are of the form: ( big_on_axes, small_on_axes ) |
| 180 | + for rectangles_on_axes in [ ("gca", "gca"), ("host", "host"), |
| 181 | + ("host", "parasite"), ("parasite", "host"), |
| 182 | + ("parasite", "parasite") ]: |
| 183 | + for click_on in [ "big", "small" ]: |
| 184 | + yield self.run, rectangles_on_axes, click_on |
| 185 | + @cleanup |
| 186 | + def run(self, rectangles_on_axes, click_on): |
| 187 | + # Axes setup |
| 188 | + axes = { "gca": None, "host": None, "parasite": None } |
| 189 | + if "gca" in rectangles_on_axes: |
| 190 | + axes["gca"] = plt.gca() |
| 191 | + if "host" in rectangles_on_axes or "parasite" in rectangles_on_axes: |
| 192 | + axes["host"] = host_subplot(111) |
| 193 | + axes["parasite"] = axes["host"].twin() |
| 194 | + # Add rectangles to axes |
| 195 | + (big_on_axes, small_on_axes) = rectangles_on_axes |
| 196 | + axes[big_on_axes].add_patch(self.big) |
| 197 | + axes[small_on_axes].add_patch(self.small) |
| 198 | + # Simulate picking with click mouse event |
| 199 | + if click_on == "big": |
| 200 | + click_axes = axes[big_on_axes] |
| 201 | + axes_coords = (0.3, 0.3) |
| 202 | + else: |
| 203 | + click_axes = axes[small_on_axes] |
| 204 | + axes_coords = (0.5, 0.5) |
| 205 | + # In reality, mouse events never happen on parasite axes, only host axes |
| 206 | + if click_axes is axes["parasite"]: |
| 207 | + click_axes = axes["host"] |
| 208 | + (x, y) = click_axes.transAxes.transform(axes_coords) |
| 209 | + m = MouseEvent("button_press_event", click_axes.figure.canvas, x, y, |
| 210 | + button=1) |
| 211 | + click_axes.pick(m) |
| 212 | + # Wait at most a second for events; actual waiting only happens if |
| 213 | + # something is wrong and tests fail, this won't slow down normal testing |
| 214 | + n_events = 2 if click_on == "small" else 1 |
| 215 | + event_rects = [] |
| 216 | + for i in range(n_events): |
| 217 | + event = self.q.get(True, 0.5) |
| 218 | + event_rects.append(event.artist) |
| 219 | + assert self.big in event_rects |
| 220 | + if click_on == "small": |
| 221 | + assert self.small in event_rects |
| 222 | + |
| 223 | + |
160 | 224 | if __name__ == '__main__':
|
161 | 225 | import nose
|
162 | 226 | nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
|
0 commit comments