|
1 | 1 | """ |
2 | | -Some examples of how to annotate various artists. |
3 | | -
|
4 | | -
|
5 | | -See matplotlib.text.Annotation for details |
| 2 | +Some examples of how to annotate points in figures. You specify an |
| 3 | +annotation point xy=(x,y) and a text point xytext=(x,y) for the |
| 4 | +annotated points and text location, respectively. Optionally, you can specify the coordinate system of xy and xytext with one of the following strings for xycoords and textcoords (default is 'data') |
| 5 | +
|
| 6 | +
|
| 7 | + 'figure points' : points from the lower left corner of the figure |
| 8 | + 'figure pixels' : pixels from the lower left corner of the figure |
| 9 | + 'figure fraction' : 0,0 is lower left of figure and 1,1 is upper, right |
| 10 | + 'axes points' : points from lower left corner of axes |
| 11 | + 'axes pixels' : pixels from lower left corner of axes |
| 12 | + 'axes fraction' : 0,1 is lower left of axes and 1,1 is upper right |
| 13 | + 'data' : use the axes data coordinate system |
| 14 | +
|
| 15 | +Optionally, you can specify a line which connects the text to the |
| 16 | +annotated point by giving a dictionary of line properties lineprops |
| 17 | +(see matplotlib.lines.Line2D for line properties) and a marker |
| 18 | +properties markerprops |
| 19 | +
|
| 20 | +For physical coordinate systems (points or pixels) the origin is the |
| 21 | +(bottom, left) of the figure or axes. If the value is negative, |
| 22 | +however, the origin is from the (right, top) of the figure or axes, |
| 23 | +analogous to negative indexing of sequences. |
6 | 24 | """ |
7 | | -from pylab import figure, show, nx |
8 | | -from matplotlib.patches import Rectangle, CirclePolygon, Ellipse |
9 | | -from matplotlib.text import Annotation |
10 | | - |
11 | | -if 1: |
12 | | - fig = figure() |
13 | | - ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1,5), ylim=(-3,5)) |
14 | | - |
15 | | - rect = Rectangle((0.5, 0.5), 1, 3, alpha=0.3) |
16 | | - ax.add_patch(rect) |
17 | | - |
18 | | - t = nx.arange(0.0, 5.0, 0.01) |
19 | | - s = nx.sin(2*nx.pi*t) |
20 | | - line, = ax.plot(t, s, lw=3, color='purple') |
21 | | - |
22 | | - a = Annotation(rect, 'A: rect', loc=('outside right', 'outside top'), |
23 | | - color='blue') |
24 | | - ax.add_artist(a) |
25 | 25 |
|
26 | | - b = Annotation(rect, 'B: rect', loc=('inside left', 'inside top'), |
27 | | - autopad=8, color='blue') |
28 | | - ax.add_artist(b) |
29 | | - |
30 | | - c = Annotation(rect, 'C: rect', loc=('center', 'center'), color='blue') |
31 | | - ax.add_artist(c) |
32 | | - |
33 | | - d = Annotation(ax, 'bottom corner', loc=('inside right', 'inside bottom'), |
34 | | - color='red', autopad=40, lineprops=dict(lw=2, color='red', shrink=4)) |
35 | | - ax.add_artist(d) |
36 | | - |
37 | | - e = Annotation(ax, 'E: an axes title', loc=('center', 'outside top'), |
38 | | - color='red') |
39 | | - ax.add_artist(e) |
40 | | - |
41 | | - f = Annotation(fig, 'F: a figure title', loc=('center', 'inside top'), |
42 | | - autopad=10, size=16, color='green') |
43 | | - ax.add_artist(f) |
44 | | - |
45 | | - g = Annotation(line, 'localmax', loc=(2.25, 1), padx=20, pady=80, |
46 | | - color='black', size=18, |
47 | | - lineprops=dict(lw=2, color='k', shrink=5., xalign='center')) |
48 | | - ax.add_artist(g) |
49 | | - |
50 | | - fig.savefig('annotation_demo') |
51 | 26 |
|
| 27 | +from pylab import figure, show, nx |
| 28 | +from matplotlib.patches import Ellipse |
52 | 29 |
|
53 | 30 | if 1: |
54 | | - # here are some annotations using various coordinate systems. If you |
55 | | - # pass in loc=(x,y) where x,y are scalars, then you can specify the |
56 | | - # following strings for the coordinate system |
57 | | - # 'figure points' : points from the lower left corner of the figure |
58 | | - # 'figure pixels' : pixels from the lower left corner of the figure |
59 | | - # 'figure fraction' : 0,0 is lower left of figure and 1,1 is upper, right |
60 | | - # 'axes points' : points from lower left corner of axes |
61 | | - # 'axes pixels' : pixels from lower left corner of axes |
62 | | - # 'axes fraction' : 0,1 is lower left of axes and 1,1 is upper right |
63 | | - # 'data' : use the coordinate system of the object being annotated (default) |
64 | | - |
65 | | - |
| 31 | + # if only one location is given, the text and xypoint being |
| 32 | + # annotated are assumed to be the same |
66 | 33 | fig = figure() |
67 | 34 | ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1,5), ylim=(-3,5)) |
68 | 35 |
|
69 | 36 | t = nx.arange(0.0, 5.0, 0.01) |
70 | | - s = nx.sin(2*nx.pi*t) |
| 37 | + s = nx.cos(2*nx.pi*t) |
71 | 38 | line, = ax.plot(t, s, lw=3, color='purple') |
72 | 39 |
|
73 | | - a = Annotation(ax, 'A: center', loc=(.5, .5), coords='axes fraction') |
74 | | - ax.add_artist(a) |
| 40 | + ax.annotate('axes center', xy=(.5, .5), xycoords='axes fraction', |
| 41 | + horizontalalignment='center', verticalalignment='center') |
75 | 42 |
|
76 | | - b = Annotation(fig, 'B: pixels', loc=(20, 20), coords='figure pixels') |
77 | | - ax.add_artist(b) |
| 43 | + ax.annotate('pixels', xy=(20, 20), xycoords='figure pixels') |
78 | 44 |
|
79 | | - c = Annotation(fig, 'C: points', loc=(100, 300), coords='figure points') |
80 | | - ax.add_artist(c) |
| 45 | + ax.annotate('points', xy=(100, 300), xycoords='figure points') |
81 | 46 |
|
82 | | - d = Annotation(line, 'D: data', loc=(1, 2), coords='data') |
83 | | - ax.add_artist(d) |
| 47 | + ax.annotate('local max', xy=(3, 1), xycoords='data', |
| 48 | + xytext=(0.9, 0.9), textcoords='axes fraction', |
| 49 | + lineprops=dict(lw=2, color='black'), |
| 50 | + markerprops=dict(marker='o', markerfacecolor='b'), |
| 51 | + horizontalalignment='right', verticalalignment='top', |
| 52 | + ) |
84 | 53 |
|
85 | | - # use positive points or pixels to specify from left, bottom |
86 | | - e = Annotation(fig, 'E: a figure title (fraction)', loc=(.05, .95), coords='figure fraction', |
87 | | - horizontalalignment='left', verticalalignment='top', |
88 | | - fontsize=20) |
89 | | - ax.add_artist(e) |
| 54 | + ax.annotate('a fractional title', xy=(.025, .975), |
| 55 | + xycoords='figure fraction', |
| 56 | + horizontalalignment='left', verticalalignment='top', |
| 57 | + fontsize=20) |
90 | 58 |
|
91 | | - # use negative points or pixels to specify from right, top |
92 | | - f = Annotation(fig, 'F: a figure title (points)', loc=(-10, -10), coords='figure points', |
93 | | - horizontalalignment='right', verticalalignment='top', |
94 | | - fontsize=20) |
95 | | - ax.add_artist(f) |
| 59 | + # use negative points or pixels to specify from right, top -10, 10 |
| 60 | + # is 10 points to the left of the right side of the axes and 10 |
| 61 | + # points above the bottom |
| 62 | + ax.annotate('bottom right (points)', xy=(-10, 10), |
| 63 | + xycoords='axes points', |
| 64 | + horizontalalignment='right', verticalalignment='bottom', |
| 65 | + fontsize=20) |
96 | 66 |
|
97 | 67 | fig.savefig('annotation_coords') |
98 | 68 |
|
99 | 69 | if 1: |
100 | | - # annotations work on polar axes too. The annotation coords below |
101 | | - # are in polar coordinates, and the pads are in physical display |
102 | | - # cartesian coordinates |
| 70 | + # you can specify the xypoint and the xytext in different |
| 71 | + # positions and coordinate systems, and optionally turn on a |
| 72 | + # connecting line and mark the point with a marker. Annotations |
| 73 | + # work on polar axes too. In the example below, the xy point is |
| 74 | + # in native coordinates (xycoords defaults to 'data'). For a |
| 75 | + # polar axes, this is in (theta, radius) space. The text in this |
| 76 | + # example is placed in the fractional figure coordinate system. |
| 77 | + # Text keyword args like horizontal and vertical alignment are |
| 78 | + # respected |
103 | 79 | fig = figure() |
104 | 80 | ax = fig.add_subplot(111, polar=True) |
105 | 81 | r = nx.arange(0,1,0.001) |
|
109 | 85 | ind = 800 |
110 | 86 | thisr, thistheta = r[ind], theta[ind] |
111 | 87 | ax.plot([thistheta], [thisr], 'o') |
112 | | - a = Annotation(line, 'a polar annotation', loc=(thistheta, thisr), |
113 | | - padx=60, pady=-30, |
114 | | - lineprops=dict(lw=2, color='k', shrink=5.)) |
115 | | - |
116 | | - ax.add_artist(a) |
117 | | - fig.savefig('annotation_polar') |
| 88 | + ax.annotate('a polar annotation', |
| 89 | + xy=(thistheta, thisr), # theta, radius |
| 90 | + xytext=(0.05, 0.05), # fraction, fraction |
| 91 | + textcoords='figure fraction', |
| 92 | + lineprops=dict(lw=2, color='k'), |
| 93 | + markerprops=dict(marker='o', markersize=6), |
| 94 | + horizontalalignment='left', |
| 95 | + verticalalignment='bottom', |
| 96 | + ) |
| 97 | + #fig.savefig('annotation_polar') |
118 | 98 |
|
119 | 99 | if 1: |
120 | | - from matplotlib.patches import Ellipse |
| 100 | + # You can also use polar notation on a catesian axes. Here the |
| 101 | + # native coordinate system ('data') is cartesian, so you need to |
| 102 | + # specify the xycoords and textcoords as 'polar' if you want to |
| 103 | + # use (theta, radius) |
121 | 104 |
|
122 | 105 | el = Ellipse((0,0), 10, 20, facecolor='r', alpha=0.5) |
123 | 106 |
|
124 | 107 | fig = figure() |
125 | 108 | ax = fig.add_subplot(111, aspect='equal') |
126 | 109 | ax.add_artist(el) |
127 | 110 | el.set_clip_box(ax.bbox) |
128 | | - ax.plot([0], [10], 'o') |
129 | | - a = Annotation(el, 'the top', loc=(nx.pi/2., 10), coords='polar', |
130 | | - padx=30, pady=30, |
131 | | - lineprops=dict(lw=2, color='k', shrink=4.)) |
132 | | - ax.add_artist(a) |
133 | | - fig.savefig('annotation_ellipse') |
134 | | - |
| 111 | + ax.annotate('the top', |
| 112 | + xy=(nx.pi/2., 10.), # theta, radius |
| 113 | + xytext=(nx.pi/4, 20.), # theta, radius |
| 114 | + xycoords='polar', |
| 115 | + textcoords='polar', |
| 116 | + lineprops=dict(lw=2, color='k'), |
| 117 | + markerprops=dict(marker='o', color='red'), |
| 118 | + horizontalalignment='left', |
| 119 | + verticalalignment='bottom', |
| 120 | + ) |
| 121 | + |
135 | 122 | ax.set_xlim(-20, 20) |
136 | 123 | ax.set_ylim(-20, 20) |
| 124 | + #fig.savefig('annotation_ellipse') |
| 125 | + |
137 | 126 |
|
138 | 127 |
|
139 | 128 | show() |
0 commit comments