|
1 | 1 | """
|
2 |
| -=================== |
3 |
| -AnnotationBbox demo |
4 |
| -=================== |
5 |
| -
|
6 |
| -`.AnnotationBbox`creates an annotation using an `.OffsetBox`, and |
7 |
| -provides more fine-grained control than `.Axes.annotate`. This example |
8 |
| -demonstrates the useofAnnotationBbox together with three different |
9 |
| -OffsetBoxes: `.TextArea`, `.DrawingArea`, and `.OffsetImage`. |
| 2 | +====================== |
| 3 | +Artists as annotations |
| 4 | +====================== |
| 5 | +
|
| 6 | +`.AnnotationBbox`facilitates annotating parts of the figure or axes using arbitrary |
| 7 | +artists, such as texts, images, and `matplotlib.patches`. `.AnnotationBbox` supports |
| 8 | +these artists via inputs that are subclassesof`.OffsetBox`, which is a class of |
| 9 | +container artists for positioning an artist relative to a parent artist. |
10 | 10 | """
|
11 | 11 |
|
12 | 12 | importmatplotlib.pyplotasplt
|
|
15 | 15 | frommatplotlib.cbookimportget_sample_data
|
16 | 16 | frommatplotlib.offsetboximport (AnnotationBbox,DrawingArea,OffsetImage,
|
17 | 17 | TextArea)
|
18 |
| -frommatplotlib.patchesimportCircle |
| 18 | +frommatplotlib.patchesimportAnnulus,Circle,ConnectionPatch |
19 | 19 |
|
20 |
| -fig,ax=plt.subplots() |
| 20 | +# %%%% |
| 21 | +# Text |
| 22 | +# ==== |
| 23 | +# |
| 24 | +# `.AnnotationBbox` supports positioning annotations relative to data, Artists, and |
| 25 | +# callables, as described in :ref:`annotations`. The `.TextArea` is used to create a |
| 26 | +# textbox that is not explicitly attached to an axes, which allows it to be used for |
| 27 | +# annotating figure objects. The `.annotate` method should be used when annotating an |
| 28 | +# axes element (such as a plot) with text. |
| 29 | +# |
| 30 | +fig,axd=plt.subplot_mosaic([['t1','.','t2']],layout='compressed') |
21 | 31 |
|
22 | 32 | # Define a 1st position to annotate (display it with a marker)
|
23 |
| -xy= (0.5,0.7) |
24 |
| -ax.plot(xy[0],xy[1],".r") |
25 |
| - |
26 |
| -# Annotate the 1st position with a text box ('Test 1') |
| 33 | +xy1= (.25,.75) |
| 34 | +xy2= (.75,.25) |
| 35 | +axd['t1'].plot(*xy1,".r") |
| 36 | +axd['t2'].plot(*xy2,".r") |
| 37 | +axd['t1'].set(xlim=(0,1),ylim=(0,1),aspect='equal') |
| 38 | +axd['t2'].set(xlim=(0,1),ylim=(0,1),aspect='equal') |
| 39 | +# Draw an arrow between the points |
| 40 | + |
| 41 | +c=ConnectionPatch(xyA=xy1,xyB=xy2, |
| 42 | +coordsA=axd['t1'].transData,coordsB=axd['t2'].transData, |
| 43 | +arrowstyle='->') |
| 44 | +fig.add_artist(c) |
| 45 | + |
| 46 | +# Annotate the ConnectionPatch position ('Test 1') |
27 | 47 | offsetbox=TextArea("Test 1")
|
28 | 48 |
|
29 |
| -ab=AnnotationBbox(offsetbox,xy, |
30 |
| -xybox=(-20,40), |
31 |
| -xycoords='data', |
32 |
| -boxcoords="offset points", |
33 |
| -arrowprops=dict(arrowstyle="->"), |
34 |
| -bboxprops=dict(boxstyle="sawtooth")) |
35 |
| -ax.add_artist(ab) |
| 49 | +ab1=AnnotationBbox(offsetbox,(.5,.5), |
| 50 | +xybox=(0,0), |
| 51 | +xycoords=c, |
| 52 | +boxcoords="offset points", |
| 53 | +arrowprops=dict(arrowstyle="->"), |
| 54 | +bboxprops=dict(boxstyle="sawtooth")) |
| 55 | +fig.add_artist(ab1) |
36 | 56 |
|
37 |
| -# Annotate the 1st position with another text box ('Test') |
38 |
| -offsetbox=TextArea("Test") |
| 57 | +# %%%% |
| 58 | +# Images |
| 59 | +# ====== |
| 60 | +# The `.OffsetImage` container supports plotting images. `.OffsetImage` accepts many of |
| 61 | +# the same parameters as other image plotting methods, such as *cmap*, *norm*, and |
| 62 | +# *interpolation*. |
39 | 63 |
|
40 |
| -ab=AnnotationBbox(offsetbox,xy, |
41 |
| -xybox=(1.02,xy[1]), |
42 |
| -xycoords='data', |
43 |
| -boxcoords=("axes fraction","data"), |
44 |
| -box_alignment=(0.,0.5), |
45 |
| -arrowprops=dict(arrowstyle="->")) |
46 |
| -ax.add_artist(ab) |
47 | 64 |
|
48 |
| -# Define a 2nd position to annotate (don't display with a marker this time) |
| 65 | +fig,ax=plt.subplots() |
| 66 | +# Define a position to annotate (don't display with a marker) |
49 | 67 | xy= [0.3,0.55]
|
50 | 68 |
|
51 |
| -# Annotate the 2nd position with a circle patch |
52 |
| -da=DrawingArea(20,20,0,0) |
53 |
| -p=Circle((10,10),10) |
54 |
| -da.add_artist(p) |
55 |
| - |
56 |
| -ab=AnnotationBbox(da,xy, |
57 |
| -xybox=(1.,xy[1]), |
58 |
| -xycoords='data', |
59 |
| -boxcoords=("axes fraction","data"), |
60 |
| -box_alignment=(0.2,0.5), |
61 |
| -arrowprops=dict(arrowstyle="->"), |
62 |
| -bboxprops=dict(alpha=0.5)) |
63 |
| - |
64 |
| -ax.add_artist(ab) |
65 |
| - |
66 |
| -# Annotate the 2nd position with an image (a generated array of pixels) |
| 69 | +# Annotate a position with an image generated from an array of pixels |
67 | 70 | arr=np.arange(100).reshape((10,10))
|
68 |
| -im=OffsetImage(arr,zoom=2) |
| 71 | +im=OffsetImage(arr,zoom=2,cmap='viridis') |
69 | 72 | im.image.axes=ax
|
70 | 73 |
|
71 | 74 | ab=AnnotationBbox(im,xy,
|
|
74 | 77 | boxcoords="offset points",
|
75 | 78 | pad=0.3,
|
76 | 79 | arrowprops=dict(arrowstyle="->"))
|
77 |
| - |
78 | 80 | ax.add_artist(ab)
|
79 | 81 |
|
80 |
| -# Annotate the2ndposition with another image (a Grace Hopper portrait) |
| 82 | +# Annotate the position with another image (a Grace Hopper portrait) |
81 | 83 | withget_sample_data("grace_hopper.jpg")asfile:
|
82 | 84 | arr_img=plt.imread(file)
|
83 | 85 |
|
|
102 | 104 |
|
103 | 105 | plt.show()
|
104 | 106 |
|
| 107 | +# %%%% |
| 108 | +# Arbitrary Artists |
| 109 | +# ================ |
| 110 | +# |
| 111 | +# Multiple and arbitrary artists can be placed inside a `.DrawingArea`. |
| 112 | + |
| 113 | + |
| 114 | +fig,ax=plt.subplots() |
| 115 | +# Define a position to annotate (don't display with a marker) |
| 116 | +xy= [0.3,0.55] |
| 117 | + |
| 118 | +# Annotate the position with a circle and annulus |
| 119 | +da=DrawingArea(30,30,0,0) |
| 120 | +p=Circle((10,10),10,color='C0') |
| 121 | +da.add_artist(p) |
| 122 | +q=Annulus((20,20),10,5,color='C1') |
| 123 | +da.add_artist(q) |
| 124 | + |
| 125 | + |
| 126 | +# Use the drawing area as an annotation |
| 127 | +ab=AnnotationBbox(da,xy, |
| 128 | +xybox=(.75,xy[1]), |
| 129 | +xycoords='data', |
| 130 | +boxcoords=("axes fraction","data"), |
| 131 | +box_alignment=(0.2,0.5), |
| 132 | +arrowprops=dict(arrowstyle="->"), |
| 133 | +bboxprops=dict(alpha=0.5)) |
| 134 | + |
| 135 | +ax.add_artist(ab) |
| 136 | +plt.show() |
| 137 | +# |
| 138 | + |
105 | 139 | # %%
|
106 | 140 | #
|
107 | 141 | # .. admonition:: References
|
|
117 | 151 | # - `matplotlib.cbook.get_sample_data`
|
118 | 152 | # - `matplotlib.pyplot.subplots`
|
119 | 153 | # - `matplotlib.pyplot.imread`
|
| 154 | +# |
| 155 | +# .. tags:: |
| 156 | +# component: annotation, styling: position |