Skip to content

Commit b5a50f5

Browse files
committed
added offset to annotations tutorial
1 parent fb60413 commit b5a50f5

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

tutorials/text/annotations.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
# For physical coordinate systems (points or pixels) the origin is the
6060
# bottom-left of the figure or axes.
6161
#
62+
# The following strings are also valid for *textcoords*
63+
# ================== ========================================================
64+
# argument coordinate system
65+
# ================== ========================================================
66+
# 'offset points' offset (in points) from the xy value
67+
# 'offset pixels' offset (in pixels) from the xy value
68+
# ================== ========================================================
69+
6270
# Optionally, you can enable drawing of an arrow from the text to the annotated
6371
# point by giving a dictionary of arrow properties in the optional keyword
6472
# argument *arrowprops*.
@@ -78,9 +86,9 @@
7886
# ==================== =====================================================
7987
#
8088
#
81-
# In the example below, the *xy* point is in native coordinates
82-
# (*xycoords* defaults to 'data'). For a polar axes, this is in
83-
# (theta, radius) space. The text in this example is placed in the
89+
# In the example below, the *xy* point is in the default coordinate system for
90+
# the axes projection (*xycoords* defaults to 'data'). For a polar axes, this
91+
# is in (theta, radius) space. The text in this example is placed in the
8492
# fractional figure coordinate system. :class:`matplotlib.text.Text`
8593
# keyword arguments like *horizontalalignment*, *verticalalignment* and
8694
# *fontsize* are passed from `~matplotlib.axes.Axes.annotate` to the
@@ -103,7 +111,27 @@
103111
#
104112
# Advanced Annotations
105113
# --------------------
106-
#
114+
#
115+
# Placing Annotations Relative to Data
116+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117+
# Annotations can be positioned at a relative offset to the *xy* input to
118+
# annotation by setting the *textcoords* kwarg to one of the offset options.
119+
# In this example, the annotations are offset 1.5 points from the given *xy*
120+
# values.
121+
122+
import matplotlib
123+
import matplotlib.pyplot as plt
124+
125+
fig, ax = plt.subplots()
126+
x = [1, 3, 5, 7, 9]
127+
y = [2, 4, 6, 8, 10]
128+
annotations = ["A", "B", "C", "D", "E"]
129+
ax.scatter(x, y, s=20)
130+
131+
for xi, yi, text in zip(x, y, annotations):
132+
ax.annotate(text, xy=(xi, yi), xycoords='data',
133+
xytext=(1.5, 1.5), textcoords='offset points')
134+
107135
# Annotating with Text with Box
108136
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109137
#

0 commit comments

Comments
 (0)