|
5 | 5 |
|
6 | 6 | Create text with different alignment and rotation. |
7 | 7 | """ |
| 8 | + |
8 | 9 | import matplotlib.pyplot as plt |
9 | 10 | import matplotlib.patches as patches |
10 | 11 |
|
11 | | -# build a rectangle in axes coords |
| 12 | +fig = plt.figure() |
| 13 | + |
12 | 14 | left, width = .25, .5 |
13 | 15 | bottom, height = .25, .5 |
14 | 16 | right = left + width |
15 | 17 | top = bottom + height |
16 | 18 |
|
17 | | -fig = plt.figure() |
18 | | -ax = fig.add_axes([0,0,1,1]) |
19 | | - |
20 | | -# axes coordinates are 0,0 is bottom left and 1,1 is upper right |
21 | | -p = patches.Rectangle( |
22 | | - (left, bottom), width, height, |
23 | | - fill=False, transform=ax.transAxes, clip_on=False |
24 | | - ) |
25 | | - |
26 | | -ax.add_patch(p) |
27 | | - |
28 | | -ax.text(left, bottom, 'left top', |
29 | | - horizontalalignment='left', |
30 | | - verticalalignment='top', |
31 | | - transform=ax.transAxes) |
32 | | - |
33 | | -ax.text(left, bottom, 'left bottom', |
34 | | - horizontalalignment='left', |
35 | | - verticalalignment='bottom', |
36 | | - transform=ax.transAxes) |
37 | | - |
38 | | -ax.text(right, top, 'right bottom', |
39 | | - horizontalalignment='right', |
40 | | - verticalalignment='bottom', |
41 | | - transform=ax.transAxes) |
42 | | - |
43 | | -ax.text(right, top, 'right top', |
44 | | - horizontalalignment='right', |
45 | | - verticalalignment='top', |
46 | | - transform=ax.transAxes) |
47 | | - |
48 | | -ax.text(right, bottom, 'center top', |
49 | | - horizontalalignment='center', |
50 | | - verticalalignment='top', |
51 | | - transform=ax.transAxes) |
52 | | - |
53 | | -ax.text(left, 0.5*(bottom+top), 'right center', |
54 | | - horizontalalignment='right', |
55 | | - verticalalignment='center', |
56 | | - rotation='vertical', |
57 | | - transform=ax.transAxes) |
58 | | - |
59 | | -ax.text(left, 0.5*(bottom+top), 'left center', |
60 | | - horizontalalignment='left', |
61 | | - verticalalignment='center', |
62 | | - rotation='vertical', |
63 | | - transform=ax.transAxes) |
64 | | - |
65 | | -ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle', |
66 | | - horizontalalignment='center', |
67 | | - verticalalignment='center', |
68 | | - fontsize=20, color='red', |
69 | | - transform=ax.transAxes) |
70 | | - |
71 | | -ax.text(right, 0.5*(bottom+top), 'centered', |
72 | | - horizontalalignment='center', |
73 | | - verticalalignment='center', |
74 | | - rotation='vertical', |
75 | | - transform=ax.transAxes) |
76 | | - |
77 | | -ax.text(left, top, 'rotated\nwith newlines', |
78 | | - horizontalalignment='center', |
79 | | - verticalalignment='center', |
80 | | - rotation=45, |
81 | | - transform=ax.transAxes) |
| 19 | +# Draw a rectangle in figure coordinates ((0, 0) is bottom left and (1, 1) is |
| 20 | +# upper right). |
| 21 | +p = patches.Rectangle((left, bottom), width, height, fill=False) |
| 22 | +fig.add_artist(p) |
| 23 | + |
| 24 | +# Figure.text (aka. plt.figtext) behaves like Axes.text (aka. plt.text), with |
| 25 | +# the sole exception that the coordinates are relative to the figure ((0, 0) is |
| 26 | +# bottom left and (1, 1) is upper right). |
| 27 | +fig.text(left, bottom, 'left top', |
| 28 | + horizontalalignment='left', verticalalignment='top') |
| 29 | +fig.text(left, bottom, 'left bottom', |
| 30 | + horizontalalignment='left', verticalalignment='bottom') |
| 31 | +fig.text(right, top, 'right bottom', |
| 32 | + horizontalalignment='right', verticalalignment='bottom') |
| 33 | +fig.text(right, top, 'right top', |
| 34 | + horizontalalignment='right', verticalalignment='top') |
| 35 | +fig.text(right, bottom, 'center top', |
| 36 | + horizontalalignment='center', verticalalignment='top') |
| 37 | +fig.text(left, 0.5*(bottom+top), 'right center', |
| 38 | + horizontalalignment='right', verticalalignment='center', |
| 39 | + rotation='vertical') |
| 40 | +fig.text(left, 0.5*(bottom+top), 'left center', |
| 41 | + horizontalalignment='left', verticalalignment='center', |
| 42 | + rotation='vertical') |
| 43 | +fig.text(0.5*(left+right), 0.5*(bottom+top), 'middle', |
| 44 | + horizontalalignment='center', verticalalignment='center', |
| 45 | + fontsize=20, color='red') |
| 46 | +fig.text(right, 0.5*(bottom+top), 'centered', |
| 47 | + horizontalalignment='center', verticalalignment='center', |
| 48 | + rotation='vertical') |
| 49 | +fig.text(left, top, 'rotated\nwith newlines', |
| 50 | + horizontalalignment='center', verticalalignment='center', |
| 51 | + rotation=45) |
82 | 52 |
|
83 | | -ax.set_axis_off() |
84 | 53 | plt.show() |
85 | 54 |
|
86 | 55 | ############################################################################# |
|
94 | 63 | # in this example: |
95 | 64 |
|
96 | 65 | import matplotlib |
97 | | -matplotlib.axes.Axes.text |
98 | | -matplotlib.pyplot.text |
| 66 | +matplotlib.figure.Figure.add_artist |
| 67 | +matplotlib.figure.Figure.text |
0 commit comments