|
1 | 1 | """
|
2 |
| -=================== |
3 |
| -Precise text layout |
4 |
| -=================== |
| 2 | +============== |
| 3 | +Text alignment |
| 4 | +============== |
| 5 | +
|
| 6 | +Texts are aligned relative to their anchor point depending on the properties |
| 7 | +``horizontalalignment`` and ``verticalalignment``. |
| 8 | +
|
| 9 | +.. plot:: |
| 10 | +
|
| 11 | + import matplotlib.pyplot as plt |
| 12 | + import numpy as np |
| 13 | +
|
| 14 | + y = [0.22, 0.34, 0.5, 0.56, 0.78] |
| 15 | + x = [0.17, 0.5, 0.855] |
| 16 | + X, Y = np.meshgrid(x, y) |
| 17 | +
|
| 18 | + fig, ax = plt.subplots(figsize=(6, 4), dpi=100) |
| 19 | + ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[]) |
| 20 | + ax.spines[:].set_visible(False) |
| 21 | + ax.text(0.5, 0.5, 'plot', fontsize=128, ha='center', va='center', zorder=1) |
| 22 | + ax.hlines(y, x[0], x[-1], color='grey') |
| 23 | + ax.vlines(x, y[0], y[-1], color='grey') |
| 24 | + ax.plot(X.ravel(), Y.ravel(), 'o') |
| 25 | + pad_x = 0.02 |
| 26 | + pad_y = 0.04 |
| 27 | + ax.text(x[0] - pad_x, y[0], 'bottom', ha='right', va='center') |
| 28 | + ax.text(x[0] - pad_x, y[1], 'baseline', ha='right', va='center') |
| 29 | + ax.text(x[0] - pad_x, y[2], 'center', ha='right', va='center') |
| 30 | + ax.text(x[0] - pad_x, y[3], 'center_baseline', ha='right', va='center') |
| 31 | + ax.text(x[0] - pad_x, y[4], 'top', ha='right', va='center') |
| 32 | + ax.text(x[0], y[0] - pad_y, 'left', ha='center', va='top') |
| 33 | + ax.text(x[1], y[0] - pad_y, 'center', ha='center', va='top') |
| 34 | + ax.text(x[2], y[0] - pad_y, 'right', ha='center', va='top') |
| 35 | + ax.set_xlabel('horizontalalignment', fontsize=14) |
| 36 | + ax.set_ylabel('verticalalignment', fontsize=14, labelpad=35) |
| 37 | + ax.set_title( |
| 38 | + 'Relative position of text anchor point depending on alignment') |
| 39 | + plt.show() |
5 | 40 |
|
6 |
| -You can precisely layout text in data or axes coordinates. This example shows |
7 |
| -you some of the alignment and rotation specifications for text layout. |
8 | 41 | """
|
9 | 42 |
|
| 43 | +############################################################################# |
| 44 | +# The following plot uses this to align text relative to a plotted rectangle. |
| 45 | + |
10 | 46 | import matplotlib.pyplot as plt
|
11 | 47 |
|
12 | 48 | fig, ax = plt.subplots()
|
|
0 commit comments