|
2 | 2 | Annotations
|
3 | 3 | ===========
|
4 | 4 |
|
5 |
| -Annotating text with Matplotlib. |
| 5 | +An annotation is a piece of text referring to a data point. While one can |
| 6 | +add a text with `~.Axes.text`, this is usually not convenient, because |
| 7 | +the text should usually not be placed exactly at the data point, requiring |
| 8 | +manual calculation of the coordinates. |
| 9 | +
|
| 10 | +`~.Axes.annotate` makes annotations easier because |
| 11 | +
|
| 12 | +- it uses explicit coordinates for both the text and the data point. This |
| 13 | + makes placing the text in a desired position easier. |
| 14 | +- it allows adding an arrow from the text to the data point. The arrow can be |
| 15 | + styled in various ways. |
6 | 16 |
|
7 | 17 | .. contents:: Table of Contents
|
8 | 18 | :depth: 3
|
|
13 | 23 | # Basic annotation
|
14 | 24 | # ----------------
|
15 | 25 | #
|
16 |
| -# The uses of the basic :func:`~matplotlib.pyplot.text` will place text |
17 |
| -# at an arbitrary position on the Axes. A common use case of text is to |
18 |
| -# annotate some feature of the plot, and the |
19 |
| -# :func:`~matplotlib.axes.Axes.annotate` method provides helper functionality |
20 |
| -# to make annotations easy. In an annotation, there are two points to |
21 |
| -# consider: the location being annotated represented by the argument |
22 |
| -# *xy* and the location of the text *xytext*. Both of these |
23 |
| -# arguments are ``(x, y)`` tuples: |
| 26 | +# In an annotation, there are two points to consider: the location being |
| 27 | +# annotated represented by the argument *xy* and the location of the text |
| 28 | +# *xytext*. Both of these arguments are ``(x, y)`` tuples: |
24 | 29 |
|
25 | 30 | import numpy as np
|
26 | 31 | import matplotlib.pyplot as plt
|
|
177 | 182 | # text:
|
178 | 183 |
|
179 | 184 | fig, ax = plt.subplots(figsize=(5, 5))
|
180 |
| -t = ax.text(0, 0, "Direction", |
| 185 | +t = ax.text(0.5, 0.5, "Direction", |
181 | 186 | ha="center", va="center", rotation=45, size=15,
|
182 |
| - bbox=dict(boxstyle="rarrow,pad=0.3", fc="cyan", ec="b", lw=2)) |
| 187 | + bbox=dict(boxstyle="rarrow,pad=0.3", |
| 188 | + fc="lightblue", ec="steelblue", lw=2)) |
183 | 189 |
|
184 | 190 | ###############################################################################
|
185 | 191 | # The arguments are the name of the box style with its attributes as
|
|
0 commit comments