Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Add text description

Many times we need to add descriptive text to the graph to highlight the importance of points or lines in the graph.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.8.1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
plt.title('Acceleration Moving')
plt.xlabel('Time')
plt.ylabel('distance')
plt.scatter(x[0],y[0])
plt.text(x[0], y[0].'start')
plt.plot(x, y, c = 'c')
plt.show()
Copy the code

The Tips: plt.text() function takes the location and text to display as arguments. The position is given in coordinates that specify the position of the lower-left corner of the text box.

Alignment of text

The text is surrounded by an implicit text box (shown below) that aligns the text relative to the coordinates passed to plt.text(). Use the Verticalalignment and Horizontalalignment parameters (which can be abbreviated va and HA, respectively) to control the alignment. The vertical alignment options are as follows:

The parameter value instructions
center Parameter coordinates relative to the center of the text box
top Parameter coordinates relative to the upper side of the text box
bottom Parameter coordinates relative to the bottom of the text box
baseline Parameter coordinates relative to the baseline of the text
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.8.1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
x_b = np.linspace(0.8.1000)
y_b = np.zeros_like(x_b)
plt.title('Acceleration Moving')
plt.xlabel('Time')
plt.ylabel('distance')
plt.scatter(x[0],y[0])
plt.text(0.0.'center', va='center')
plt.text(2.0.'top', va='top')
plt.text(4.0.'bottom', va='bottom')
plt.text(6.0.'baseline', va='baseline')
plt.plot(x, y, c = 'c')
plt.plot(x_b, y_b, c = 'm')
plt.show()
Copy the code

The horizontal alignment options are as follows:

The parameter value instructions
center Parameter coordinates relative to the center of the text box
left Parameter coordinates relative to the left of the text box
right Parameter coordinates relative to the right of the text box
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.8.1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
y_b = np.linspace(0.100.1000)
x_b = np.zeros_like(y_b)
plt.title('Acceleration Moving')
plt.xlabel('Time')
plt.ylabel('distance')
plt.scatter(x[0],y[0])
plt.text(0.0.'center', ha='center')
plt.text(0.50.'left', ha='left')
plt.text(0.100.'right', ha='right')
plt.plot(x, y, c = 'c')
plt.plot(x_b, y_b, c = 'm')
plt.show()
Copy the code

Text bounding box

In order to explicitly draw a text box, plt.plot() supports a bbox in the form of a dictionary, which defines the appearance configuration of the text box:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.8.1000)
y = 2.0 * x + 0.5 * 5 * x ** 2
box = {
    'facecolor': '75'.'edgecolor': 'r'.'boxstyle': 'round'
}
plt.title('Acceleration Moving')
plt.text(0.8.'start', bbox=box)
plt.plot(x, y, c = 'c')
plt.show()
Copy the code

The dictionary definition for the bbox argument contains the following common key-value pairs:

key Description and optional values
facecolor Used to set the color of the text box background and edge
edgecolor Used to set the color of the text box edge
alpha Used to set the transparency level to better blend the text box with the background
boxstyle Sets the style of the text box. Options include “round” and “square”
pad If “boxstyle” is set to “square”, it defines the amount of padding between the text and the edges of the text box

Series of links

Matplotlib common statistical graph drawing

Matplotlib uses custom colors to draw statistics

Matplotlib controls line style and line width

Matplotlib custom style to draw beautiful statistics