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

Skip to content

Commit ed7285e

Browse files
productivememberofsociety666smheidrich
productivememberofsociety666
authored andcommitted
Added tests for picking on host and parasite axes.
1 parent 8ccffba commit ed7285e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
AnchoredSizeBar,
1818
AnchoredDirectionArrows)
1919

20+
from matplotlib.backend_bases import MouseEvent
2021
from matplotlib.colors import LogNorm
2122
from matplotlib.transforms import Bbox, TransformedBbox, \
2223
blended_transform_factory
@@ -348,3 +349,64 @@ def test_anchored_direction_arrows_many_args():
348349
sep_x=-0.06, sep_y=-0.08, back_length=0.1, head_width=9,
349350
head_length=10, tail_width=5)
350351
ax.add_artist(direction_arrows)
352+
353+
354+
class TestPickingCallbacksOverlap(object):
355+
"""Test pick events on normal, host or parasite axes."""
356+
# Two rectangles are drawn and "clicked on", a small one and a big one
357+
# enclosing the small one. The axis on which they are drawn as well as the
358+
# rectangle that is clicked on are varied.
359+
# In each case we expect that both rectangles are picked if we click on the
360+
# small one and only the big one is picked if we click on the big one.
361+
# Also tests picking on normal axes ("gca") as a control.
362+
def setUp(self):
363+
self.q = six.Queue()
364+
self.big = plt.Rectangle((0.25, 0.25), 0.5, 0.5, picker=5)
365+
self.small = plt.Rectangle((0.4, 0.4), 0.2, 0.2, facecolor="r",
366+
picker=5)
367+
plt.gcf().canvas.mpl_connect('pick_event', self.on_pick)
368+
def on_pick(self, event):
369+
self.q.put(event)
370+
def test_picking_simple(self):
371+
# Configurations here are of the form: ( big_on_axes, small_on_axes )
372+
for rectangles_on_axes in [ ("gca", "gca"), ("host", "host"),
373+
("host", "parasite"), ("parasite", "host"),
374+
("parasite", "parasite") ]:
375+
for click_on in [ "big", "small" ]:
376+
yield self.run, rectangles_on_axes, click_on
377+
def run(self, rectangles_on_axes, click_on):
378+
# Axes setup
379+
axes = { "gca": None, "host": None, "parasite": None }
380+
if "gca" in rectangles_on_axes:
381+
axes["gca"] = plt.gca()
382+
if "host" in rectangles_on_axes or "parasite" in rectangles_on_axes:
383+
axes["host"] = host_subplot(111)
384+
axes["parasite"] = axes["host"].twin()
385+
# Add rectangles to axes
386+
(big_on_axes, small_on_axes) = rectangles_on_axes
387+
axes[big_on_axes].add_patch(self.big)
388+
axes[small_on_axes].add_patch(self.small)
389+
# Simulate picking with click mouse event
390+
if click_on == "big":
391+
click_axes = axes[big_on_axes]
392+
axes_coords = (0.3, 0.3)
393+
else:
394+
click_axes = axes[small_on_axes]
395+
axes_coords = (0.5, 0.5)
396+
# In reality, mouse events never happen on parasite axes, only host axes
397+
if click_axes is axes["parasite"]:
398+
click_axes = axes["host"]
399+
(x, y) = click_axes.transAxes.transform(axes_coords)
400+
m = MouseEvent("button_press_event", click_axes.figure.canvas, x, y,
401+
button=1)
402+
click_axes.pick(m)
403+
# Wait at most a second for events; actual waiting only happens if
404+
# something is wrong and tests fail, this won't slow down normal testing
405+
n_events = 2 if click_on == "small" else 1
406+
event_rects = []
407+
for i in range(n_events):
408+
event = self.q.get(True, 0.5)
409+
event_rects.append(event.artist)
410+
assert self.big in event_rects
411+
if click_on == "small":
412+
assert self.small in event_rects

0 commit comments

Comments
 (0)