3.5 Python data visualization - customization of chart auxiliary elements

3.5 Add reference line and reference area

3.5.1 Add reference line
A reference line is one or more lines that run through the drawing area and are used to facilitate comparisons between graph data in the drawing area.
Reference basis, commonly used in financial analysis and business analysis, such as target line, average line, budget line, etc. Reference Lines by Direction
The difference can be divided into horizontal guide lines and vertical guide lines. The axhline() and axvline() functions are provided in matplotlib, respectively
It is used to add horizontal guide lines and vertical guide lines. The details are as follows.
1. Use axhline() to draw horizontal guide lines
The syntax format of the axhline() function is as follows:
axhline(y=0, xmin=0, xmax=1, linestyle='-', ** kwargs)
The meanings of common parameters of this function are as follows.
·y : Indicates the vertical coordinate of the horizontal reference line.

·xmin : Indicates the starting position of the horizontal reference line, the default is 0.

·xmax : Indicates the end position of the horizontal reference line, the default is 1.
·linestyle : Indicates the type of horizontal reference line, the default is solid line.
2. Use axvline() to draw vertical guide lines
The syntax format of the axvline() function is as follows:
axvline(x=0, ymin=0, ymax=1, linestyle='-', ** kwargs)
The meanings of common parameters of this function are as follows.
·x: Indicates the abscissa of the vertical reference line.
·ymin : Indicates the starting position of the vertical reference line, the default is 0.
·ymax : Indicates the end position of the vertical reference line, the default is 1.
·linestyle : Indicates the type of vertical reference line, the default is solid line.
Add reference lines to the sine and cosine graphs drawn in Section 3.4.1, the added code is as follows.
#add reference line
plt.axvline(x=0, linestyle='--')
plt.axhline(y=0, linestyle='--')
The above code sets the type of the reference line as a dotted line through the linestyle parameter, which avoids confusion between the reference line and the curve.
Line types are described in Chapter 4.
Run the program, the effect is shown in Figure 3-11.
3.5.2 Add reference area
The axhspan() and axvspan() functions are provided in the pyplot module, which are used to add a horizontal reference area and a vertical reference area to the chart, respectively.
The direct reference area is as follows.
1. Use axhspan() to draw a horizontal reference area
The syntax of the axhspan() function is as follows:
axhspan(ymin, ymax, xmin=0, xmax=1, ** kwargs)

The meanings of common parameters of this function are as follows.

·ymin : Indicates the lower limit of the horizontal span, in units of data.
·ymax: Indicates the upper limit of the horizontal span, in units of data.
xmin : Indicates the lower limit of the vertical span, in units of the axis, and defaults to 0.
· xmax : Indicates the upper limit of the vertical span, in axis units, defaults to 1.
2. Use axvspan() to draw a vertical reference area
The syntax of the axvspan() function is as follows:
axvspan(xmin, xmax, ymin=0, ymax=1, ** kwargs)
The meanings of common parameters of this function are as follows.
· xmin : Indicates the lower limit of the vertical span.
· xmax : Indicates the upper limit of the vertical span.
Add a reference area to the sine and cosine graphs drawn in section 3.5.1, the added code is as follows.
# add reference area
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)
Run the program, the effect is shown in Figure 3-12.
3.5.3 Example 4: Evaluation of English scores of male and female students in each class of the second grade in the whole school
After the mid-term mock exam in the second grade of a senior high school, the school makes statistics on the average grades of each class and subject in the grade, and calculates
The average English score of all senior two students is 88.5, and the average English scores of boys and girls in each class of senior two are shown in Table 3-3.

According to the data in Table 3-3, use the data in the "Class Name" column as the scale label of the x- axis, and set "Boys" and "Girls"
The two columns of data are used as the values ​​corresponding to the scale labels, and bar() is used to draw the column charts of the average English scores of boys and girls in each class.
Use the average English score of the second grade of high school as a reference line to compare the English scores of which classes need to be improved. The specific codes are as follows.
In [5]:
# 04_average_score_of_english
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False  
men_means = (90.5, 89.5, 88.7, 88.5, 85.2, 86.6)
women_means = (92.7, 87.0, 90.5, 85.0, 89.5, 89.8)
ind = np.arange(len(men_means)) #The x position of each column
width = 0.2             #The width of each column
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(ind - width / 2, men_means, width, label=' average grades of boys ')
ax.bar(ind + 0.2, women_means, width, label=' means of girls ')
ax.set_title(' Average scores of boys and girls in each class of senior high school ')
ax.set_ylabel(' score ')
ax.set_xticks(ind)
ax.set_xticklabels([' Grade 2 Class 1 ', ' Grade 2 Class 2 ', ' Grade 2 Class 3 ', ' Grade 2 Class 4 ' ,
      ' Grade 2 Class 5 ', ' Grade 2 Class 6 '])
#add reference line
ax.axhline(88.5, ls='--', linewidth=1.0, label=' overall average score ')
ax.legend(loc="lower right")
plt.show()
Run the program, the effect is shown in Figure 3-13.
In Figure 3-13, the blue dotted line represents the average English score of the second grade. It can be seen from Figure 3-13 that Class 2, Class 4
The average grades of girls in Class 5 and boys in Class 5 and 6 are lower than the average grades of English in the second grade.

Guess you like

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