|
113 | 113 | import matplotlib.patches as mpatches |
114 | 114 |
|
115 | 115 | fig, ax = plt.subplots(figsize=(3, 3)) |
116 | | -arr = mpatches.FancyArrowPatch((1.25, 1.25), (1.75, 1.75), |
| 116 | +arr = mpatches.FancyArrowPatch((1.25, 1.5), (1.75, 1.5), |
117 | 117 | arrowstyle='->,head_width=.15', mutation_scale=20) |
118 | 118 | ax.add_patch(arr) |
119 | | -ax.annotate("label", (.5, .5), xycoords=arr, ha='center', va='center') |
| 119 | +ax.annotate("label", (.5, .5), xycoords=arr, ha='center', va='bottom') |
120 | 120 | ax.set(xlim=(1, 2), ylim=(1, 2)) |
121 | 121 |
|
122 | 122 | # %% |
123 | 123 | # Here the annotation is placed at position (.5,.5) relative to the arrow's |
124 | | -# lower left corner and is vertically and horizontally centered at that |
125 | | -# position. For an example of chaining annotation Artists, see the |
| 124 | +# lower left corner and is vertically and horizontally at that position. |
| 125 | +# Vertically, the bottom aligns to that reference point so that the label |
| 126 | +# is above the line. For an example of chaining annotation Artists, see the |
126 | 127 | # :ref:`Artist section <artist_annotation_coord>` of |
127 | 128 | # :ref:`annotating_coordinate_systems`. |
128 | 129 | # |
@@ -568,23 +569,25 @@ def custom_box_style(x0, y0, width, height, mutation_size): |
568 | 569 | # coordinate system to "axes fraction": |
569 | 570 |
|
570 | 571 | fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(6, 3)) |
571 | | -ax1.annotate("Test", xy=(0.5, 0.5), xycoords=ax1.transAxes) |
572 | | -ax2.annotate("Test", xy=(0.5, 0.5), xycoords="axes fraction") |
| 572 | +ax1.annotate("Test", xy=(0.2, 0.2), xycoords=ax1.transAxes) |
| 573 | +ax2.annotate("Test", xy=(0.2, 0.2), xycoords="axes fraction") |
573 | 574 |
|
574 | 575 | # %% |
575 | 576 | # Another commonly used `.Transform` instance is ``Axes.transData``. This |
576 | 577 | # transform is the coordinate system of the data plotted in the axes. In this |
577 | | -# example, it is used to draw an arrow from a point in *ax1* to text in *ax2*, |
578 | | -# where the point and text are positioned relative to the coordinates of *ax1* |
579 | | -# and *ax2* respectively: |
| 578 | +# example, it is used to draw an arrow between related data points in two |
| 579 | +# Axes. We have passed an empty text because in this case, the annotation |
| 580 | +# connects data points. |
580 | 581 |
|
581 | | -fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(6, 3)) |
| 582 | +x = np.linspace(-1, 1) |
582 | 583 |
|
583 | | -ax1.annotate("Test1", xy=(0.5, 0.5), xycoords="axes fraction") |
584 | | -ax2.annotate("Test2", |
585 | | - xy=(0.5, 0.5), xycoords=ax1.transData, |
586 | | - xytext=(0.5, 0.5), textcoords=ax2.transData, |
587 | | - arrowprops=dict(arrowstyle="->")) |
| 584 | +fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(6, 3)) |
| 585 | +ax1.plot(x, -x**3) |
| 586 | +ax2.plot(x, -3*x**2) |
| 587 | +ax2.annotate("", |
| 588 | + xy=(0, 0), xycoords=ax1.transData, |
| 589 | + xytext=(0, 0), textcoords=ax2.transData, |
| 590 | + arrowprops=dict(arrowstyle="<->")) |
588 | 591 |
|
589 | 592 | # %% |
590 | 593 | # .. _artist_annotation_coord: |
|
0 commit comments