8000 Merge pull request #25532 from timhoffm/annotations-tutorial · matplotlib/matplotlib@73f3a8b · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

10000
Appearance settings

Commit 73f3a8b

Browse files
authored
Merge pull request #25532 from timhoffm/annotations-tutorial
Annotations tutorial
2 parents 4b42d7f + 41ccf2b commit 73f3a8b

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

galleries/users_explain/text/annotations.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,17 @@
113113
import matplotlib.patches as mpatches
114114

115115
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),
117117
arrowstyle='->,head_width=.15', mutation_scale=20)
118118
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')
120120
ax.set(xlim=(1, 2), ylim=(1, 2))
121121

122122
# %%
123123
# 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
126127
# :ref:`Artist section <artist_annotation_coord>` of
127128
# :ref:`annotating_coordinate_systems`.
128129
#
@@ -568,23 +569,25 @@ def custom_box_style(x0, y0, width, height, mutation_size):
568569
# coordinate system to "axes fraction":
569570

570571
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")
573574

574575
# %%
575576
# Another commonly used `.Transform` instance is ``Axes.transData``. This
576577
# 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.
580581

581-
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(6, 3))
582+
x = np.linspace(-1, 1)
582583

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="<->"))
588591

589592
# %%
590593
# .. _artist_annotation_coord:

0 commit comments

Comments
 (0)
0