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

Skip to content

Commit 96591d1

Browse files
author
productivememberofsociety666
committed
Added tests for picking on host and parasite axes.
1 parent 689ca4d commit 96591d1

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
1414
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
1515

16+
from matplotlib.backend_bases import MouseEvent
17+
from matplotlib.externals.six.moves.queue import Queue
1618
from matplotlib.colors import LogNorm
1719
from itertools import product
1820

@@ -156,6 +158,68 @@ def get_demo_image():
156158
ax.add_artist(asb)
157159

158160

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

0 commit comments

Comments
 (0)