1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import matplotlib.pyplot as plt import numpy as np
from matplotlib.patches import Circle from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage, AnnotationBbox) from matplotlib.cbook import get_sample_data
if 1: fig, ax = plt.subplots()
xy = (0.5, 0.7) ax.plot(xy[0], xy[1], ".r")
offsetbox = TextArea("Test 1", minimumdescent=False)
ab = AnnotationBbox(offsetbox, xy, xybox=(-20, 40), xycoords='data', boxcoords="offset points", arrowprops=dict(arrowstyle="->")) ax.add_artist(ab)
offsetbox = TextArea("Test", minimumdescent=False)
ab = AnnotationBbox(offsetbox, xy, xybox=(1.02, xy[1]), xycoords='data', boxcoords=("axes fraction", "data"), box_alignment=(0., 0.5), arrowprops=dict(arrowstyle="->")) ax.add_artist(ab)
xy = [0.3, 0.55]
da = DrawingArea(20, 20, 0, 0) p = Circle((10, 10), 10) da.add_artist(p)
ab = AnnotationBbox(da, xy, xybox=(1.02, xy[1]), xycoords='data', boxcoords=("axes fraction", "data"), box_alignment=(0., 0.5), arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
arr = np.arange(100).reshape((10, 10)) im = OffsetImage(arr, zoom=2) im.image.axes = ax
ab = AnnotationBbox(im, xy, xybox=(-50., 50.), xycoords='data', boxcoords="offset points", pad=0.3, arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
fn = get_sample_data("grace_hopper.png", asfileobj=False) arr_img = plt.imread(fn, format='png')
imagebox = OffsetImage(arr_img, zoom=0.2) imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, xy, xybox=(120., -80.), xycoords='data', boxcoords="offset points", pad=0.5, arrowprops=dict( arrowstyle="->", connectionstyle="angle,angleA=0,angleB=90,rad=3") )
ax.add_artist(ab)
ax.set_xlim(0, 1) ax.set_ylim(0, 1)
plt.show()
|