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

Skip to content

Commit 34223ab

Browse files
author
productivememberofsociety666
committed
Added tests for picking on host and parasite axes.
1 parent 1467e2f commit 34223ab

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import matplotlib
77
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
911
from mpl_toolkits.axes_grid1 import make_axes_locatable, host_subplot
1012
from itertools import product
1113
import numpy as np
@@ -82,6 +84,69 @@ def test_twin_axes_empty_and_removed():
8284
horizontalalignment="center", verticalalignment="center")
8385
plt.subplots_adjust(wspace=0.5, hspace=1)
8486

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+
85150
if __name__ == '__main__':
86151
import nose
87152
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)