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