(child_type, boxcoords)
| 193 | ['axes fraction', 'axes pixels', 'axes points', |
| 194 | 'data']) |
| 195 | def test_picking(child_type, boxcoords): |
| 196 | # These all take up approximately the same area. |
| 197 | if child_type == 'draw': |
| 198 | picking_child = DrawingArea(5, 5) |
| 199 | picking_child.add_artist(mpatches.Rectangle((0, 0), 5, 5, linewidth=0)) |
| 200 | elif child_type == 'image': |
| 201 | im = np.ones((5, 5)) |
| 202 | im[2, 2] = 0 |
| 203 | picking_child = OffsetImage(im) |
| 204 | elif child_type == 'text': |
| 205 | picking_child = TextArea('\N{Black Square}', textprops={'fontsize': 5}) |
| 206 | else: |
| 207 | assert False, f'Unknown picking child type {child_type}' |
| 208 | |
| 209 | fig, ax = plt.subplots() |
| 210 | ab = AnnotationBbox(picking_child, (0.5, 0.5), boxcoords=boxcoords) |
| 211 | ab.set_picker(True) |
| 212 | ax.add_artist(ab) |
| 213 | |
| 214 | calls = [] |
| 215 | fig.canvas.mpl_connect('pick_event', lambda event: calls.append(event)) |
| 216 | |
| 217 | # Annotation should be picked by an event occurring at its center. |
| 218 | if boxcoords == 'axes points': |
| 219 | x, y = ax.transAxes.transform_point((0, 0)) |
| 220 | x += 0.5 * fig.dpi / 72 |
| 221 | y += 0.5 * fig.dpi / 72 |
| 222 | elif boxcoords == 'axes pixels': |
| 223 | x, y = ax.transAxes.transform_point((0, 0)) |
| 224 | x += 0.5 |
| 225 | y += 0.5 |
| 226 | else: |
| 227 | x, y = ax.transAxes.transform_point((0.5, 0.5)) |
| 228 | fig.canvas.draw() |
| 229 | calls.clear() |
| 230 | MouseEvent( |
| 231 | "button_press_event", fig.canvas, x, y, MouseButton.LEFT)._process() |
| 232 | assert len(calls) == 1 and calls[0].artist == ab |
| 233 | |
| 234 | # Annotation should *not* be picked by an event at its original center |
| 235 | # point when the limits have changed enough to hide the *xy* point. |
| 236 | ax.set_xlim(-1, 0) |
| 237 | ax.set_ylim(-1, 0) |
| 238 | fig.canvas.draw() |
| 239 | calls.clear() |
| 240 | MouseEvent( |
| 241 | "button_press_event", fig.canvas, x, y, MouseButton.LEFT)._process() |
| 242 | assert len(calls) == 0 |
| 243 | |
| 244 | |
| 245 | @image_comparison(['anchoredtext_align.png'], remove_text=True, style='mpl20') |
nothing calls this directly
no test coverage detected
searching dependent graphs…