| r.set_bounds( | |
| tt.get_window_extent(renderer) | |
| .transformed(ax.transAxes.inverted()) | |
| .bounds) | |
| ax.add_patch(r) | |
| horal = 'left' | |
| for nn, st in enumerate(sts): | |
| tt = ax.text(0.2 * nn + 0.1, 0.5, st, horizontalalignment=horal, | |
| verticalalignment='bottom') | |
| draw_box(ax, tt) | |
| ax.text(1.2, 0.5, 'Bottom align', color='C2') | |
| ax.axhline(1.3, color='C2', linewidth=0.3) | |
| for nn, st in enumerate(sts): | |
| tt = ax.text(0.2 * nn + 0.1, 1.3, st, horizontalalignment=horal, | |
| verticalalignment='top') | |
| draw_box(ax, tt) | |
| ax.text(1.2, 1.3, 'Top align', color='C2') | |
| ax.axhline(1.8, color='C2', linewidth=0.3) | |
| for nn, st in enumerate(sts): | |
| tt = ax.text(0.2 * nn + 0.1, 1.8, st, horizontalalignment=horal, | |
| verticalalignment='baseline') | |
| draw_box(ax, tt) | |
| ax.text(1.2, 1.8, 'Baseline align', color='C2') | |
| ax.axhline(0.1, color='C2', linewidth=0.3) | |
| for nn, st in enumerate(sts): | |
| tt = ax.text(0.2 * nn + 0.1, 0.1, st, horizontalalignment=horal, | |
| verticalalignment='bottom', rotation=20) | |
| draw_box(ax, tt) | |
| ax.text(1.2, 0.1, 'Bot align, rot20', color='C2') | |
| @image_comparison(['antialiased.png'], style='mpl20') | |
| def test_antialiasing(): | |
| mpl.rcParams['text.antialiased'] = False # Passed arguments should override. | |
| fig = plt.figure(figsize=(5.25, 0.75)) | |
| fig.text(0.3, 0.75, "antialiased", horizontalalignment='center', | |
| verticalalignment='center', antialiased=True) | |
| fig.text(0.3, 0.25, r"$\sqrt{x}$", horizontalalignment='center', | |
| verticalalignment='center', antialiased=True) | |
| mpl.rcParams['text.antialiased'] = True # Passed arguments should override. | |
| fig.text(0.7, 0.75, "not antialiased", horizontalalignment='center', | |
| verticalalignment='center', antialiased=False) | |
| fig.text(0.7, 0.25, r"$\sqrt{x}$", horizontalalignment='center', | |
| verticalalignment='center', antialiased=False) | |
| mpl.rcParams['text.antialiased'] = False # Should not affect existing text. | |
| def test_afm_kerning(): | |
| fn = mpl.font_manager.findfont("Helvetica", fontext="afm") | |
| with open(fn, 'rb') as fh: | |
| afm = mpl._afm.AFM(fh) | |
| assert afm.string_width_height('VAVAVAVAVAVA') == (7174.0, 718) | |
| @image_comparison(['text_contains.png']) | |
| def test_contains(): | |
| fig = plt.figure() | |
| ax = plt.axes() | |
| mevent = MouseEvent('button_press_event', fig.canvas, 0.5, 0.5, 1, None) | |
| xs = np.linspace(0.25, 0.75, 30) | |
| ys = np.linspace(0.25, 0.75, 30) | |
| xs, ys = np.meshgrid(xs, ys) | |
| txt = plt.text( | |
| 0.5, 0.4, 'hello world', ha='center', fontsize=30, rotation=30) | |
| # uncomment to draw the text's bounding box | |
| # txt.set_bbox(dict(edgecolor='black', facecolor='none')) | |
| # draw the text. This is important, as the contains method can only work | |
| # when a renderer exists. | |
| fig.canvas.draw() | |
| for x, y in zip(xs.flat, ys.flat): | |
| mevent.x, mevent.y = plt.gca().transAxes.transform([x, y]) | |
| contains, _ = txt.contains(mevent) | |
| color = 'yellow' if contains else 'red' | |
| # capture the viewLim, plot a point, and reset the viewLim | |
| vl = ax.viewLim.frozen() | |
| ax.plot(x, y, 'o', color=color) | |
| ax.viewLim.set(vl) | |
| def test_annotation_contains(): | |
| # Check that Annotation.contains looks at the bboxes of the text and the | |
| # arrow separately, not at the joint bbox. | |
| fig, ax = plt.subplots() | |
| ann = ax.annotate( | |
| "hello", xy=(.4, .4), xytext=(.6, .6), arrowprops={"arrowstyle": "->"}) | |
| fig.canvas.draw() # Needed for the same |