3.6 Python data visualization - add annotation text

Annotation text is an important part of the chart, it can give a brief description of the graph and help users understand the chart.
According to different annotation objects, annotation text is mainly divided into pointing type annotation text and non-pointing type annotation text, among which pointing type annotation
The text is generally a specific description for a certain part of the chart, and the undirected annotation text is generally a specific description for the whole chart.
bright. The methods for adding directed and undirected comment texts are described below.
3.6.1 Add pointing comment text
The pointing annotation text refers to the text explaining the graphics in the drawing area through the annotation method of the pointing arrow.
Lines are generally used to connect the description point and the annotation text pointed by the arrow. The annotate() function is provided in the pyplot module to add
Add pointed comment text, the syntax format of this function is as follows:
annotate(s, xy, * args, ** kwargs)
The meanings of common parameters of this function are as follows.
s : Indicates the content of the comment text.
· xy : Indicates the coordinate position of the annotated point, receiving a tuple (x, y).
·xytext: Indicates the coordinate position of the annotation text, receiving tuple (x, y).
·xycoords: Indicates the coordinate system of xy, the default value is "data", which means the same coordinate system as the line chart.
arrowprops : A dictionary of properties indicating arrows.
· bbox : A dictionary of border attributes representing annotation text.
The arrowprops parameter receives a dictionary containing several keys, and controls the display of the arrow by adding key-value pairs to the dictionary
Show. Common keys for controlling arrows include width, headwidth, headlength, shrink, arrowstyle, etc., where the key
arrowstyle represents the type of the arrow, and the corresponding value and type of the key are shown in Figure 3-14.

Add point-to-point comment text in the sine and cosine graphs drawn in Section 3.5.2, and the added code is as follows.

#Add pointing comment text
plt.annotate(" Minimum value ",
        xy=(-np.pi / 2, -1.0),
        xytext=(-(np.pi / 2), -0.5),
        arrowprops=dict(arrowstyle="->"))
Run the program, the effect is shown in Figure 3-15.
3.6.2 Adding undirected comment text
Undirected annotation text refers to the text that uses only text annotations to explain the graphics in the drawing area.
The pyplot module provides the text() function to add non-pointing comment text to the chart. The syntax of this function is as follows
Shown:
text(x, y, s, fontdict=None, withdash=<deprecated parameter>, ** kwargs)
The meanings of common parameters of this function are as follows.
· x, y : Indicates the position of the annotation text.
s : Indicates the content of the comment text.
· fontdict: represents the dictionary that controls the font.
· bbox : A dictionary of border attributes representing annotation text.
·horizontalalignment or ha: Indicates the way of horizontal alignment, which can be center, right or left.
Verticalalignment or va: Indicates the way of vertical alignment, which can be center, top, bottom, baseline
or center_baseline.
Add undirected annotation text to the sine and cosine graphs drawn in section 3.6.1, the added code is as follows.
#Add undirected comment text
plt.text(3.10, 0.10, "y=sin(x)", bbox=dict(alpha=0.2))
Run the program, the effect is shown in Figure 3-16.
Learn a trick: Matplotlib writes mathematical expressions
Matplotlib has its own mathtext engine, through which it can automatically identify and use the annotate() or text() function to pass
The input mathematical string is parsed into a corresponding mathematical expression. Math strings have a fixed format, which requires strings to start with
The dollar sign " $ " is the first and last characters, and the middle of the first and last characters is a mathematical expression. The basic format is as follows:
'$ mathematical expression $'
In order to ensure that all characters in the string can be displayed literally, the math string needs to be used with "r". under
is a simple math string written using matplotlib:
r'$\alpha > \beta$'
"\alpha" and "\beta" in the above strings correspond to common lowercase Greek letters α and β , and their corresponding mathematical expressions
The formula is as follows:
a > b
In addition, superscripts and subscripts can be added after "\alpha" and "\beta", where the superscript is represented by the symbol "^",
Subscripts are indicated by the symbol "_". For example, an example of setting the subscript of α to i and the subscript of β to i is as follows:
r'$\alpha_i > \beta_i$'
The mathematical expression corresponding to the above example is as follows:
a i > b i
Use "\frac{}{}" in matplotlib to write a number string in the form of a fraction, and the two big characters after "\frac"
The brackets represent the numerator and denominator of the fraction respectively. The sample code is as follows:
r'$\frac{3}{4}$'
The mathematical expression corresponding to the above example is as follows:

3 /4

In addition, you can also write mathematical strings with nested fractions, the code is as follows:
r'$\frac{5 - \frac{1}{x}}{4}$'
The mathematical expression corresponding to the above example is as follows:
5 1
x
4

Guess you like

Origin blog.csdn.net/qq_43416206/article/details/132262929