|
59 | 59 | # For physical coordinate systems (points or pixels) the origin is the
|
60 | 60 | # bottom-left of the figure or axes.
|
61 | 61 | #
|
| 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 | + |
62 | 70 | # Optionally, you can enable drawing of an arrow from the text to the annotated
|
63 | 71 | # point by giving a dictionary of arrow properties in the optional keyword
|
64 | 72 | # argument *arrowprops*.
|
|
78 | 86 | # ==================== =====================================================
|
79 | 87 | #
|
80 | 88 | #
|
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 |
84 | 92 | # fractional figure coordinate system. :class:`matplotlib.text.Text`
|
85 | 93 | # keyword arguments like *horizontalalignment*, *verticalalignment* and
|
86 | 94 | # *fontsize* are passed from `~matplotlib.axes.Axes.annotate` to the
|
|
103 | 111 | #
|
104 | 112 | # Advanced Annotations
|
105 | 113 | # --------------------
|
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 | + |
107 | 135 | # Annotating with Text with Box
|
108 | 136 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
109 | 137 | #
|
|
0 commit comments