|
1 | 1 | import matplotlib.pyplot as plt |
2 | | -from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, \ |
3 | | - AnnotationBbox |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from matplotlib.patches import Circle |
| 5 | +from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage, |
| 6 | + AnnotationBbox) |
4 | 7 | from matplotlib.cbook import get_sample_data |
5 | 8 |
|
6 | | -import numpy as np |
7 | 9 |
|
8 | 10 | if 1: |
9 | 11 | fig, ax = plt.subplots() |
10 | 12 |
|
11 | | - offsetbox = TextArea("Test 1", minimumdescent=False) |
12 | | - |
| 13 | + # Define a 1st position to annotate (display it with a marker) |
13 | 14 | xy = (0.5, 0.7) |
14 | | - |
15 | 15 | ax.plot(xy[0], xy[1], ".r") |
16 | 16 |
|
| 17 | + # Annotate the 1st position with a text box ('Test 1') |
| 18 | + offsetbox = TextArea("Test 1", minimumdescent=False) |
| 19 | + |
17 | 20 | ab = AnnotationBbox(offsetbox, xy, |
18 | 21 | xybox=(-20, 40), |
19 | 22 | xycoords='data', |
20 | 23 | boxcoords="offset points", |
21 | 24 | arrowprops=dict(arrowstyle="->")) |
22 | 25 | ax.add_artist(ab) |
23 | 26 |
|
| 27 | + # Annotate the 1st position with another text box ('Test') |
24 | 28 | offsetbox = TextArea("Test", minimumdescent=False) |
25 | 29 |
|
26 | 30 | ab = AnnotationBbox(offsetbox, xy, |
|
31 | 35 | arrowprops=dict(arrowstyle="->")) |
32 | 36 | ax.add_artist(ab) |
33 | 37 |
|
34 | | - from matplotlib.patches import Circle |
| 38 | + # Define a 2nd position to annotate (don't display with a marker this time) |
| 39 | + xy = [0.3, 0.55] |
| 40 | + |
| 41 | + # Annotate the 2nd position with a circle patch |
35 | 42 | da = DrawingArea(20, 20, 0, 0) |
36 | 43 | p = Circle((10, 10), 10) |
37 | 44 | da.add_artist(p) |
38 | 45 |
|
39 | | - xy = [0.3, 0.55] |
40 | 46 | ab = AnnotationBbox(da, xy, |
41 | 47 | xybox=(1.02, xy[1]), |
42 | 48 | xycoords='data', |
|
46 | 52 |
|
47 | 53 | ax.add_artist(ab) |
48 | 54 |
|
| 55 | + # Annotate the 2nd position with an image (a generated array of pixels) |
49 | 56 | arr = np.arange(100).reshape((10, 10)) |
50 | 57 | im = OffsetImage(arr, zoom=2) |
51 | 58 | im.image.axes = ax |
|
59 | 66 |
|
60 | 67 | ax.add_artist(ab) |
61 | 68 |
|
62 | | - # another image |
63 | | - |
64 | | - from matplotlib._png import read_png |
| 69 | + # Annotate the 2nd position with another image (a Grace Hopper portrait) |
65 | 70 | fn = get_sample_data("grace_hopper.png", asfileobj=False) |
66 | | - arr_img = read_png(fn) |
| 71 | + arr_img = plt.imread(fn, format='png') |
67 | 72 |
|
68 | 73 | imagebox = OffsetImage(arr_img, zoom=0.2) |
69 | 74 | imagebox.image.axes = ax |
|
73 | 78 | xycoords='data', |
74 | 79 | boxcoords="offset points", |
75 | 80 | pad=0.5, |
76 | | - arrowprops=dict(arrowstyle="->", |
77 | | - connectionstyle="angle,angleA=0,angleB=90,rad=3") |
| 81 | + arrowprops=dict( |
| 82 | + arrowstyle="->", |
| 83 | + connectionstyle="angle,angleA=0,angleB=90,rad=3") |
78 | 84 | ) |
79 | 85 |
|
80 | 86 | ax.add_artist(ab) |
81 | 87 |
|
| 88 | + # Fix the display limits to see everything |
82 | 89 | ax.set_xlim(0, 1) |
83 | 90 | ax.set_ylim(0, 1) |
84 | 91 |
|
85 | | - plt.draw() |
86 | 92 | plt.show() |
0 commit comments