|
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 use of AnnotationBbox together with three different |
9 | | -OffsetBoxes: `.TextArea`, `.DrawingArea`, and `.OffsetImage`. |
| 2 | +====================== |
| 3 | +Artists as annotations |
| 4 | +====================== |
| 5 | +
|
| 6 | +`.AnnotationBbox` facilitates using arbitrary artists as annotations, i.e. data at |
| 7 | +position *xy* is annotated by a box containing an artist at position *xybox*. The |
| 8 | +coordinate systems for these points are set via the *xycoords* and *boxcoords* |
| 9 | +parameters, respectively; see the *xycoords* and *textcoords* parameters of |
| 10 | +`.Axes.annotate` for a full listing of supported coordinate systems. |
| 11 | +The box containing the artist is a subclass of `.OffsetBox`, which is a container |
| 12 | +artist for positioning an artist relative to a parent artist. |
10 | 13 | """ |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import PIL |
11 | 17 |
|
12 | 18 | import matplotlib.pyplot as plt |
13 | 19 | import numpy as np |
14 | 20 |
|
15 | | -from matplotlib.cbook import get_sample_data |
| 21 | +from matplotlib import get_data_path |
16 | 22 | from matplotlib.offsetbox import AnnotationBbox, DrawingArea, OffsetImage, TextArea |
17 | | -from matplotlib.patches import Circle |
| 23 | +from matplotlib.patches import Annulus, Circle, ConnectionPatch |
18 | 24 |
|
19 | | -fig, ax = plt.subplots() |
| 25 | +# %%%% |
| 26 | +# Text |
| 27 | +# ==== |
| 28 | +# |
| 29 | +# `.AnnotationBbox` supports positioning annotations relative to data, Artists, and |
| 30 | +# callables, as described in :ref:`annotations`. The `.TextArea` is used to create a |
| 31 | +# textbox that is not explicitly attached to an axes, which allows it to be used for |
| 32 | +# annotating figure objects. When annotating an axes element (such as a plot) with text, |
| 33 | +# use `.Axes.annotate` because it will create the text artist for you. |
20 | 34 |
|
21 | | -# Define a 1st position to annotate (display it with a marker) |
22 | | -xy = (0.5, 0.7) |
23 | | -ax.plot(xy[0], xy[1], ".r") |
| 35 | +fig, axd = plt.subplot_mosaic([['t1', '.', 't2']], layout='compressed') |
24 | 36 |
|
25 | | -# Annotate the 1st position with a text box ('Test 1') |
| 37 | +# Define a 1st position to annotate (display it with a marker) |
| 38 | +xy1 = (.25, .75) |
| 39 | +xy2 = (.75, .25) |
| 40 | +axd['t1'].plot(*xy1, ".r") |
| 41 | +axd['t2'].plot(*xy2, ".r") |
| 42 | +axd['t1'].set(xlim=(0, 1), ylim=(0, 1), aspect='equal') |
| 43 | +axd['t2'].set(xlim=(0, 1), ylim=(0, 1), aspect='equal') |
| 44 | + |
| 45 | +# Draw a connection patch arrow between the points |
| 46 | +c = ConnectionPatch(xyA=xy1, xyB=xy2, |
| 47 | + coordsA=axd['t1'].transData, coordsB=axd['t2'].transData, |
| 48 | + arrowstyle='->') |
| 49 | +fig.add_artist(c) |
| 50 | + |
| 51 | +# Annotate the ConnectionPatch position ('Test 1') |
26 | 52 | offsetbox = TextArea("Test 1") |
27 | 53 |
|
28 | | -ab = AnnotationBbox(offsetbox, xy, |
29 | | - xybox=(-20, 40), |
30 | | - xycoords='data', |
31 | | - boxcoords="offset points", |
32 | | - arrowprops=dict(arrowstyle="->"), |
33 | | - bboxprops=dict(boxstyle="sawtooth")) |
34 | | -ax.add_artist(ab) |
35 | | - |
36 | | -# Annotate the 1st position with another text box ('Test') |
37 | | -offsetbox = TextArea("Test") |
38 | | - |
39 | | -ab = AnnotationBbox(offsetbox, xy, |
40 | | - xybox=(1.02, xy[1]), |
41 | | - xycoords='data', |
42 | | - boxcoords=("axes fraction", "data"), |
43 | | - box_alignment=(0., 0.5), |
44 | | - arrowprops=dict(arrowstyle="->")) |
45 | | -ax.add_artist(ab) |
46 | | - |
47 | | -# Define a 2nd position to annotate (don't display with a marker this time) |
48 | | -xy = [0.3, 0.55] |
49 | | - |
50 | | -# Annotate the 2nd position with a circle patch |
51 | | -da = DrawingArea(20, 20, 0, 0) |
52 | | -p = Circle((10, 10), 10) |
53 | | -da.add_artist(p) |
54 | | - |
55 | | -ab = AnnotationBbox(da, xy, |
56 | | - xybox=(1., xy[1]), |
57 | | - xycoords='data', |
58 | | - boxcoords=("axes fraction", "data"), |
59 | | - box_alignment=(0.2, 0.5), |
60 | | - arrowprops=dict(arrowstyle="->"), |
61 | | - bboxprops=dict(alpha=0.5)) |
| 54 | +# place the annotation above the midpoint of c |
| 55 | +ab1 = AnnotationBbox(offsetbox, |
| 56 | + xy=(.5, .5), |
| 57 | + xybox=(0, 30), |
| 58 | + xycoords=c, |
| 59 | + boxcoords="offset points", |
| 60 | + arrowprops=dict(arrowstyle="->"), |
| 61 | + bboxprops=dict(boxstyle="sawtooth")) |
| 62 | +fig.add_artist(ab1) |
| 63 | + |
| 64 | +# %%%% |
| 65 | +# Images |
| 66 | +# ====== |
| 67 | +# The `.OffsetImage` container facilitates using images as annotations |
62 | 68 |
|
63 | | -ax.add_artist(ab) |
| 69 | +fig, ax = plt.subplots() |
| 70 | +# Define a position to annotate |
| 71 | +xy = (0.3, 0.55) |
| 72 | +ax.scatter(*xy, s=200, marker='X') |
64 | 73 |
|
65 | | -# Annotate the 2nd position with an image (a generated array of pixels) |
| 74 | +# Annotate a position with an image generated from an array of pixels |
66 | 75 | arr = np.arange(100).reshape((10, 10)) |
67 | | -im = OffsetImage(arr, zoom=2) |
| 76 | +im = OffsetImage(arr, zoom=2, cmap='viridis') |
68 | 77 | im.image.axes = ax |
69 | 78 |
|
70 | | -ab = AnnotationBbox(im, xy, |
| 79 | +# place the image NW of xy |
| 80 | +ab = AnnotationBbox(im, xy=xy, |
71 | 81 | xybox=(-50., 50.), |
72 | 82 | xycoords='data', |
73 | 83 | boxcoords="offset points", |
74 | 84 | pad=0.3, |
75 | 85 | arrowprops=dict(arrowstyle="->")) |
76 | | - |
77 | 86 | ax.add_artist(ab) |
78 | 87 |
|
79 | | -# Annotate the 2nd position with another image (a Grace Hopper portrait) |
80 | | -with get_sample_data("grace_hopper.jpg") as file: |
81 | | - arr_img = plt.imread(file) |
| 88 | +# Annotate the position with an image from file (a Grace Hopper portrait) |
| 89 | +img_fp = Path(get_data_path(), "sample_data", "grace_hopper.jpg") |
| 90 | +with PIL.Image.open(img_fp) as arr_img: |
| 91 | + imagebox = OffsetImage(arr_img, zoom=0.2) |
82 | 92 |
|
83 | | -imagebox = OffsetImage(arr_img, zoom=0.2) |
84 | 93 | imagebox.image.axes = ax |
85 | 94 |
|
86 | | -ab = AnnotationBbox(imagebox, xy, |
| 95 | +# place the image SE of xy |
| 96 | +ab = AnnotationBbox(imagebox, xy=xy, |
87 | 97 | xybox=(120., -80.), |
88 | 98 | xycoords='data', |
89 | 99 | boxcoords="offset points", |
|
96 | 106 | ax.add_artist(ab) |
97 | 107 |
|
98 | 108 | # Fix the display limits to see everything |
99 | | -ax.set_xlim(0, 1) |
100 | | -ax.set_ylim(0, 1) |
| 109 | +ax.set(xlim=(0, 1), ylim=(0, 1)) |
| 110 | + |
| 111 | +plt.show() |
| 112 | + |
| 113 | +# %%%% |
| 114 | +# Arbitrary Artists |
| 115 | +# ================= |
| 116 | +# |
| 117 | +# Multiple and arbitrary artists can be placed inside a `.DrawingArea`. |
| 118 | + |
| 119 | +# make this the thumbnail image |
| 120 | +# sphinx_gallery_thumbnail_number = 3 |
| 121 | +fig, ax = plt.subplots() |
| 122 | + |
| 123 | +# Define a position to annotate |
| 124 | +xy = (0.05, 0.5) |
| 125 | +ax.scatter(*xy, s=500, marker='X') |
| 126 | + |
| 127 | +# Annotate the position with a circle and annulus |
| 128 | +da = DrawingArea(120, 120) |
| 129 | +p = Circle((30, 30), 25, color='C0') |
| 130 | +da.add_artist(p) |
| 131 | +q = Annulus((65, 65), 50, 5, color='C1') |
| 132 | +da.add_artist(q) |
| 133 | + |
| 134 | + |
| 135 | +# Use the drawing area as an annotation |
| 136 | +ab = AnnotationBbox(da, xy=xy, |
| 137 | + xybox=(.55, xy[1]), |
| 138 | + xycoords='data', |
| 139 | + boxcoords=("axes fraction", "data"), |
| 140 | + box_alignment=(0, 0.5), |
| 141 | + arrowprops=dict(arrowstyle="->"), |
| 142 | + bboxprops=dict(alpha=0.5)) |
| 143 | + |
| 144 | +ax.add_artist(ab) |
| 145 | + |
| 146 | +# Fix the display limits to see everything |
| 147 | +ax.set(xlim=(0, 1), ylim=(0, 1)) |
101 | 148 |
|
102 | 149 | plt.show() |
103 | 150 |
|
|
108 | 155 | # The use of the following functions, methods, classes and modules is shown |
109 | 156 | # in this example: |
110 | 157 | # |
111 | | -# - `matplotlib.patches.Circle` |
112 | 158 | # - `matplotlib.offsetbox.TextArea` |
113 | 159 | # - `matplotlib.offsetbox.DrawingArea` |
114 | 160 | # - `matplotlib.offsetbox.OffsetImage` |
115 | 161 | # - `matplotlib.offsetbox.AnnotationBbox` |
116 | | -# - `matplotlib.cbook.get_sample_data` |
117 | | -# - `matplotlib.pyplot.subplots` |
118 | | -# - `matplotlib.pyplot.imread` |
| 162 | +# |
| 163 | +# .. tags:: |
| 164 | +# component: annotation, styling: position |
0 commit comments