|
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`.Thisexample |
8 |
| -demonstrates the use of AnnotationBbox together with three different |
9 |
| -OffsetBoxes: `.TextArea`, `.DrawingArea`, and `.OffsetImage`. |
| 2 | +====================== |
| 3 | +Artists as annotations |
| 4 | +====================== |
| 5 | +
|
| 6 | +`.AnnotationBbox` creates an annotation using an `.OffsetBox` object, which is a class |
| 7 | +of container artists for positioning an artist relative to a parent artist.Thisallows |
| 8 | +for annotations that are texts, images, and arbitrary artists. `.AnnotationBbox` also |
| 9 | +provides more fine-grained control than `.Axes.annotate`. |
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 |
19 | 19 |
|
| 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. |
| 27 | +# |
20 | 28 | fig,ax=plt.subplots()
|
21 | 29 |
|
22 | 30 | # Define a 1st position to annotate (display it with a marker)
|
|
26 | 34 | # Annotate the 1st position with a text box ('Test 1')
|
27 | 35 | offsetbox=TextArea("Test 1")
|
28 | 36 |
|
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) |
| 37 | +ab1=AnnotationBbox(offsetbox,xy, |
| 38 | +xybox=(-20,40), |
| 39 | +xycoords='data', |
| 40 | +boxcoords="offset points", |
| 41 | +arrowprops=dict(arrowstyle="->"), |
| 42 | +bboxprops=dict(boxstyle="sawtooth")) |
| 43 | +ax.add_artist(ab1) |
36 | 44 |
|
37 | 45 | # Annotate the 1st position with another text box ('Test')
|
38 |
| -offsetbox=TextArea("Test") |
39 |
| - |
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) |
| 46 | +offsetbox=TextArea("Test 2") |
47 | 47 |
|
48 |
| -# Define a 2nd position to annotate (don't display with a marker this time) |
49 |
| -xy= [0.3,0.55] |
| 48 | +ab2=AnnotationBbox(offsetbox, (1,.85), |
| 49 | +xybox=(.75,xy[1]), |
| 50 | +xycoords=ab1, |
| 51 | +boxcoords=("axes fraction","data"), |
| 52 | +box_alignment=(0.,0.5), |
| 53 | +arrowprops=dict(arrowstyle="->")) |
| 54 | +ax.add_artist(ab2) |
50 | 55 |
|
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) |
| 56 | +#%%%% |
| 57 | +# Images |
| 58 | +# ====== |
| 59 | +# The `.OffsetImage` container supports plotting images using `.BboxImage` |
55 | 60 |
|
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) |
| 61 | +fig,ax=plt.subplots() |
| 62 | +# Define a position to annotate (don't display with a marker) |
| 63 | +xy= [0.3,0.55] |
65 | 64 |
|
66 |
| -# Annotatethe 2ndposition with an image(agenerated array of pixels) |
| 65 | +# Annotateaposition with an image generatedfrom anarray of pixels |
67 | 66 | arr=np.arange(100).reshape((10,10))
|
68 |
| -im=OffsetImage(arr,zoom=2) |
| 67 | +im=OffsetImage(arr,zoom=2,cmap='viridis') |
69 | 68 | im.image.axes=ax
|
70 | 69 |
|
71 | 70 | ab=AnnotationBbox(im,xy,
|
|
74 | 73 | boxcoords="offset points",
|
75 | 74 | pad=0.3,
|
76 | 75 | arrowprops=dict(arrowstyle="->"))
|
77 |
| - |
78 | 76 | ax.add_artist(ab)
|
79 | 77 |
|
80 |
| -# Annotate the2ndposition with another image (a Grace Hopper portrait) |
| 78 | +# Annotate the position with another image (a Grace Hopper portrait) |
81 | 79 | withget_sample_data("grace_hopper.jpg")asfile:
|
82 | 80 | arr_img=plt.imread(file)
|
83 | 81 |
|
|
102 | 100 |
|
103 | 101 | plt.show()
|
104 | 102 |
|
| 103 | +# %%%% |
| 104 | +# Arbitrary Artists |
| 105 | +# ================ |
| 106 | +# `.DrawingArea` artists position arbitrary artists relative to their parent artists. |
| 107 | + |
| 108 | +fig,ax=plt.subplots() |
| 109 | +# Define a position to annotate (don't display with a marker) |
| 110 | +xy= [0.3,0.55] |
| 111 | + |
| 112 | +# Annotate the position with a circle and annulus |
| 113 | +da=DrawingArea(30,30,0,0) |
| 114 | +p=Circle((10,10),10,color='C0') |
| 115 | +da.add_artist(p) |
| 116 | +q=Annulus((20,20),10,5,color='C1') |
| 117 | +da.add_artist(q) |
| 118 | + |
| 119 | + |
| 120 | +# Use the drawing area as an annotation |
| 121 | +ab=AnnotationBbox(da,xy, |
| 122 | +xybox=(.75,xy[1]), |
| 123 | +xycoords='data', |
| 124 | +boxcoords=("axes fraction","data"), |
| 125 | +box_alignment=(0.2,0.5), |
| 126 | +arrowprops=dict(arrowstyle="->"), |
| 127 | +bboxprops=dict(alpha=0.5)) |
| 128 | + |
| 129 | +ax.add_artist(ab) |
| 130 | +plt.show() |
| 131 | +# |
| 132 | + |
105 | 133 | # %%
|
106 | 134 | #
|
107 | 135 | # .. admonition:: References
|
|
117 | 145 | # - `matplotlib.cbook.get_sample_data`
|
118 | 146 | # - `matplotlib.pyplot.subplots`
|
119 | 147 | # - `matplotlib.pyplot.imread`
|
| 148 | +# |
| 149 | +# .. tags:: |
| 150 | +# component: annotation, styling: position |