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 monthThe 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 sectorsToo 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_alipayimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = Falsekinds = [' 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 titleplt.title(' Alipay monthly bill report ')#add legendplt.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-17Compared 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 Grid3.4.1 Display grid with specified styleA grid is an auxiliary line that extends from the tick marks and runs through the entire drawing area, which helps people easily seeThe 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 followsas 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 gridplt.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 beAccurately see the value. Therefore, this example will display the grid in the scatter plot and adjust the scale of the coordinate axis. The specific codeas follows.In [4]:# 03_vehicle_speed_and_braking_distanceimport numpy as npimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = 'SimHei'plt.rcParams['axes.unicode_minus'] = Falsex_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- axisplt.xlabel(' speed (km/h)')
plt.ylabel(' braking distance (m)')plt.xticks(x_speed)# show the gridplt.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 roughlyFind out what value each data point corresponds to.
3.3.3 Python data visualization - customization of chart auxiliary elements
Guess you like
Origin blog.csdn.net/qq_43416206/article/details/132262835
Ranking