3.3.3 Python data visualization - customization of chart auxiliary elements

3.3.3 Example 2: Alipay monthly bill report (add title, legend)
Legends are commonly found in pie charts, and are mainly used to mark the meaning of each sector in the pie chart. User A in section 2.6.2 a certain month
The pie chart of the Alipay bill report marks the meaning of each sector outside the circle, because the length of the marked text varies and the number of sectors
Too many, resulting in a messy chart, so move all the label text in the pie chart to the legend, the specific code is as follows.
In [3]:
# 02_monthly_bills_of_alipay
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
kinds = [' shopping ', ' human relationship ', ' food and beverage ', ' communication logistics ', ' daily life ',
      ' Transportation ', ' Recreation ', ' Others ']
money_scale = [800 / 3000, 100 / 3000, 1000 / 3000, 200 / 3000,
      300 / 3000, 200 / 3000, 200 / 3000, 200 / 3000]

dev_position = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]

plt.pie(money_scale, autopct='%3.1f%%', shadow=True,
      explode=dev_position, startangle=90)
#add title
plt.title(' Alipay monthly bill report ')
#add legend
plt.legend(kinds, loc='upper right', bbox_to_anchor=[1.3, 1.1])
plt.show()
Run the program, the effect is shown in Figure 3-8.
In Figure 3-8, the title is positioned at the top of the graph and is center-aligned with the graph, and the legend is positioned at the top right of the graph. with Figure 2-17

Compared with Figure 3-8, the title and legend are added to help users clarify the meaning of the pie chart and its sector of each color.

3.4 Display Grid
3.4.1 Display grid with specified style
A grid is an auxiliary line that extends from the tick marks and runs through the entire drawing area, which helps people easily see
The numerical value of the graph. Grids can be divided into vertical grids and horizontal grids according to different directions. These two grids can be used alone,
It can also be used at the same time, which is often used in scenes that add chart precision and distinguish graphic nuances.
In matplotlib, the grid () function of the pyplot module can be used directly to display the grid. The syntax of the grid () function is as follows
as shown below:
grid(b=None, which='major', axis='both', ** kwargs)
The meanings of common parameters of this function are as follows.

· b : Indicates whether to display the grid. If other keyword arguments are provided, the b parameter is set to True.

·which : Indicates the type of display grid, supports major, minor, and both, and defaults to major.
·axis : Indicates which direction to display the grid, this parameter supports 3 options of both, x and y, the default is both.
·linewidth or lw: Indicates the width of the grid line.
Alternatively, a grid can be displayed using the grid() method of the Axes object. It should be noted that if there is no scale on the coordinate axis,
The grid cannot be displayed.
To display the horizontal grid in the sine and cosine plots drawn in Section 3.3.2, add the following code.
# show the grid
plt.grid(b=True, axis='y', linewidth=0.3)
Run the program, the effect is shown in Figure 3-9.
3.4.2 Example 3: Relationship between car speed and braking distance (add grid)
In the scatter diagram of the relationship between vehicle speed and braking distance in Section 2.7.2, many dots cannot be
Accurately see the value. Therefore, this example will display the grid in the scatter plot and adjust the scale of the coordinate axis. The specific code
as follows.
In [4]:
# 03_vehicle_speed_and_braking_distance
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
x_speed = np.arange(10, 210, 10)
y_distance = np.array([0.5, 2.0, 4.4, 7.9, 12.3,
              17.7, 24.1, 31.5, 39.9, 49.2,
              59.5, 70.8, 83.1, 96.4, 110.7,
              126.0, 142.2, 159.4, 177.6, 196.8])
plt.scatter(x_speed, y_distance, s=50, alpha=0.9, linewidths=0.3)
#Set the label and tick label of the x- axis

plt.xlabel(' speed (km/h)')

plt.ylabel(' braking distance (m)')
plt.xticks(x_speed)
# show the grid
plt.grid(b=True, linewidth=0.3)
plt.show()
Run the program, the effect is shown in Figure 3-10.
Compared with Figure 2-19, the scatter chart in Figure 3-10 has added coordinate axis labels and light gray grids, which help users roughly
Find out what value each data point corresponds to.

Guess you like

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