|
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 |
|
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 | +fig, ax = plt.subplots() |
| 123 | +x = [1, 3, 5, 7, 9] |
| 124 | +y = [2, 4, 6, 8, 10] |
| 125 | +annotations = ["A", "B", "C", "D", "E"] |
| 126 | +ax.scatter(x, y, s=20) |
| 127 | + |
| 128 | +for xi, yi, text in zip(x, y, annotations): |
| 129 | + ax.annotate(text, xy=(xi, yi), xycoords='data', |
| 130 | + xytext=(1.5, 1.5), textcoords='offset points') |
| 131 | + |
107 | 132 | # Annotating with Text with Box |
108 | 133 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
109 | 134 | # |
|
0 commit comments