def test_picking(child_type, boxcoords): # These all take up approximately the same area. if child_type == 'draw': picking_child = DrawingArea(5, 5) picking_child.add_artist(mpatches.Rectangle((0, 0), 5, 5, linewidth=0)) elif child_type == 'image': im = np.ones((5, 5)) im[2, 2] = 0 picking_child = OffsetImage(im) elif child_type == 'text': picking_child = TextArea('\N{Black Square}', textprops={'fontsize': 5}) else: assert False, f'Unknown picking child type {child_type}' fig, ax = plt.subplots() ab = AnnotationBbox(picking_child, (0.5, 0.5), boxcoords=boxcoords) ab.set_picker(True) ax.add_artist(ab) calls = [] fig.canvas.mpl_connect('pick_event', lambda event: calls.append(event)) # Annotation should be picked by an event occurring at its center. if boxcoords == 'axes points': x, y = ax.transAxes.transform_point((0, 0)) x += 0.5 * fig.dpi / 72 y += 0.5 * fig.dpi / 72 elif boxcoords == 'axes pixels': x, y = ax.transAxes.transform_point((0, 0)) x += 0.5 y += 0.5 else: x, y = ax.transAxes.transform_point((0.5, 0.5)) fig.canvas.draw() calls.clear() fig.canvas.button_press_event(x, y, MouseButton.LEFT) assert len(calls) == 1 and calls[0].artist == ab # Annotation should *not* be picked by an event at its original center # point when the limits have changed enough to hide the *xy* point. ax.set_xlim(-1, 0) ax.set_ylim(-1, 0) fig.canvas.draw() calls.clear() fig.canvas.button_press_event(x, y, MouseButton.LEFT) assert len(calls) == 0
def add_image(self, color): """Draw an image Parameters ---------- color : string. specify the color of the image to draw. The concatenation of the self.img_path and the color will give the complete path to the image. (ex: to draw a network in blue, self.img_path should='../ressources/network.png' and color='blue') """ if self.artist: self.artist.remove() self.color = color file_name, ext = os.path.splitext(self.img_path) new_path = file_name + color + ext image_path = get_sample_data(new_path) image = plt.imread(image_path) im = OffsetImage(image, zoom=self.zoom) ab = AnnotationBbox(im, (self.x, self.y), xycoords='data', frameon=False) ab.set_picker(5) self.artist = plt.gca().add_artist(ab) self.annotation_box = ab