|
| 1 | +import matplotlib.pyplot as plt |
| 2 | + |
| 3 | +fig = plt.figure(1) |
| 4 | +fig.clf() |
| 5 | + |
| 6 | +ax1 = plt.subplot(121) |
| 7 | +ax2 = plt.subplot(122) |
| 8 | + |
| 9 | +bbox_args = dict(boxstyle="round", fc="0.8") |
| 10 | +arrow_args = dict(arrowstyle="->") |
| 11 | + |
| 12 | +ax1.annotate('figure fraction : 0, 0', xy=(0, 0), xycoords='figure fraction', |
| 13 | + xytext=(20, 20), textcoords='offset points', |
| 14 | + ha="left", va="bottom", |
| 15 | + bbox=bbox_args, |
| 16 | + arrowprops=arrow_args |
| 17 | + ) |
| 18 | + |
| 19 | +ax1.annotate('figure fraction : 1, 1', xy=(1, 1), xycoords='figure fraction', |
| 20 | + xytext=(-20, -20), textcoords='offset points', |
| 21 | + ha="right", va="top", |
| 22 | + bbox=bbox_args, |
| 23 | + arrowprops=arrow_args |
| 24 | + ) |
| 25 | + |
| 26 | +ax1.annotate('axes fraction : 0, 0', xy=(0, 0), xycoords='axes fraction', |
| 27 | + xytext=(20, 20), textcoords='offset points', |
| 28 | + ha="left", va="bottom", |
| 29 | + bbox=bbox_args, |
| 30 | + arrowprops=arrow_args |
| 31 | + ) |
| 32 | + |
| 33 | +ax1.annotate('axes fraction : 1, 1', xy=(1, 1), xycoords='axes fraction', |
| 34 | + xytext=(-20, -20), textcoords='offset points', |
| 35 | + ha="right", va="top", |
| 36 | + bbox=bbox_args, |
| 37 | + arrowprops=arrow_args |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +an1 = ax1.annotate('Drag me 1', xy=(.5, .7), xycoords='data', |
| 42 | + #xytext=(.5, .7), textcoords='data', |
| 43 | + ha="center", va="center", |
| 44 | + bbox=bbox_args, |
| 45 | + #arrowprops=arrow_args |
| 46 | + ) |
| 47 | + |
| 48 | +an2 = ax1.annotate('Drag me 2', xy=(.5, .5), xycoords=an1, |
| 49 | + xytext=(.5, .3), textcoords='axes fraction', |
| 50 | + ha="center", va="center", |
| 51 | + bbox=bbox_args, |
| 52 | + arrowprops=dict(patchB=an1.get_bbox_patch(), |
| 53 | + connectionstyle="arc3,rad=0.2", |
| 54 | + **arrow_args) |
| 55 | + ) |
| 56 | + |
| 57 | +an3 = ax1.annotate('', xy=(.5, .5), xycoords=an2, |
| 58 | + xytext=(.5, .5), textcoords=an1, |
| 59 | + ha="center", va="center", |
| 60 | + bbox=bbox_args, |
| 61 | + arrowprops=dict(patchA=an1.get_bbox_patch(), |
| 62 | + patchB=an2.get_bbox_patch(), |
| 63 | + connectionstyle="arc3,rad=0.2", |
| 64 | + **arrow_args) |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +t = ax2.annotate('xy=(0, 1)\nxycoords=("data", "axes fraction")', |
| 70 | + xy=(0, 1), xycoords=("data", 'axes fraction'), |
| 71 | + xytext=(0, -20), textcoords='offset points', |
| 72 | + ha="center", va="top", |
| 73 | + bbox=bbox_args, |
| 74 | + arrowprops=arrow_args |
| 75 | + ) |
| 76 | + |
| 77 | +from matplotlib.text import OffsetFrom |
| 78 | + |
| 79 | +ax2.annotate('xy=(0.5, 0)\nxycoords="bbox fraction"\nxybbox=artist', |
| 80 | + xy=(0.5, 0.), xycoords=t.get_window_extent, |
| 81 | + xytext=(0, -20), textcoords='offset points', |
| 82 | + ha="center", va="top", |
| 83 | + bbox=bbox_args, |
| 84 | + arrowprops=arrow_args |
| 85 | + ) |
| 86 | + |
| 87 | +ax2.annotate('xy=(0.8, 0.5)\nxycoords="bbox"\nxybbox=ax1.transData', |
| 88 | + xy=(0.8, 0.5), xycoords=ax1.transData, |
| 89 | + #xytext=(0, 0), textcoords='data', |
| 90 | + xytext=(10, 10), textcoords=OffsetFrom(ax2.bbox, (0, 0), "points"), |
| 91 | + ha="left", va="bottom", |
| 92 | + bbox=bbox_args, |
| 93 | + arrowprops=arrow_args |
| 94 | + ) |
| 95 | + |
| 96 | +ax2.set_xlim(-2, 2) |
| 97 | +ax2.set_ylim(-2, 2) |
| 98 | + |
| 99 | +an1.draggable() |
| 100 | +an2.draggable() |
| 101 | + |
| 102 | +plt.show() |
0 commit comments