()
| 209 | |
| 210 | @image_comparison(['text_contains.png'], style='mpl20') |
| 211 | def test_contains(): |
| 212 | fig = plt.figure(figsize=(8, 6)) |
| 213 | ax = plt.axes() |
| 214 | |
| 215 | mevent = MouseEvent('button_press_event', fig.canvas, 0.5, 0.5, 1, None) |
| 216 | |
| 217 | xs = np.linspace(0.25, 0.75, 30) |
| 218 | ys = np.linspace(0.25, 0.75, 30) |
| 219 | xs, ys = np.meshgrid(xs, ys) |
| 220 | |
| 221 | txt = plt.text( |
| 222 | 0.5, 0.4, 'hello world', ha='center', fontsize=30, rotation=30) |
| 223 | # uncomment to draw the text's bounding box |
| 224 | # txt.set_bbox(dict(edgecolor='black', facecolor='none')) |
| 225 | |
| 226 | # draw the text. This is important, as the contains method can only work |
| 227 | # when a renderer exists. |
| 228 | fig.canvas.draw() |
| 229 | |
| 230 | for x, y in zip(xs.flat, ys.flat): |
| 231 | mevent.x, mevent.y = plt.gca().transAxes.transform([x, y]) |
| 232 | contains, _ = txt.contains(mevent) |
| 233 | color = 'yellow' if contains else 'red' |
| 234 | |
| 235 | # capture the viewLim, plot a point, and reset the viewLim |
| 236 | vl = ax.viewLim.frozen() |
| 237 | ax.plot(x, y, 'o', color=color) |
| 238 | ax.viewLim.set(vl) |
| 239 | |
| 240 | |
| 241 | def test_annotation_contains(): |
nothing calls this directly
no test coverage detected
searching dependent graphs…