Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba03e48

Browse files
committed
Minor improvements to Annotations Tutorial
- Move the basic text() vs. annotate() discussion to the top - Styling in "Annotating with boxed text": move the text from Axes edge to center; less glaring colors
1 parent 799f735 commit ba03e48

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

tutorials/text/annotations.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
Annotations
33
===========
44
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.
616
717
.. contents:: Table of Contents
818
:depth: 3
@@ -13,14 +23,9 @@
1323
# Basic annotation
1424
# ----------------
1525
#
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:
2429

2530
import numpy as np
2631
import matplotlib.pyplot as plt
@@ -177,9 +182,10 @@
177182
# text:
178183

179184
fig, ax = plt.subplots(figsize=(5, 5))
180-
t = ax.text(0, 0, "Direction",
185+
t = ax.text(0.5, 0.5, "Direction",
181186
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))
183189

184190
###############################################################################
185191
# The arguments are the name of the box style with its attributes as

0 commit comments

Comments
 (0)