2.8Python Data Visualization----Using matplotlib to draw simple graphs

2.8 Draw a box plot

2.8.1 Draw a box plot using boxplot()

Use the boxplot() function of pyplot to quickly draw box plots. The syntax of the boxplot() function is as follows:
boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None,

      widths=None, patch_artist=None, bootstrap=None, usermedians=None,

   conf_intervals=None, meanline=None, showmeans=None, showcaps=None,
       showbox=None, showfliers=None, boxprops=None, labels=None,
       flierprops=None, medianprops=None, meanprops=None, capprops=None,
       whiskerprops=None, manage_ticks=True, autorange=False,
       zorder=None, * , data=None)
The common parameters of this function have the following meanings:
· x : Draw the data of the box plot.
·sym : Indicates the symbol corresponding to the outlier, and the default is a hollow circle.
·vert : Indicates whether to place the box plot vertically, and the default is to place it vertically.
·whis : Indicates the distance between the upper and lower whiskers of the box plot and the upper and lower quartiles, the default is 1.5 times the quartile difference.
·positions: indicates the position of the cabinet.
·widths : Indicates the width of the box, the default is 0.5.
·patch_artist: indicates whether to fill the color of the box, the default is not filled.
·meanline: whether to mark the median with a line across the box, it is not used by default.
·showcaps: Indicates whether to display the horizontal lines at the top and bottom of the cabinet, and it is displayed by default.
·showbox: Indicates whether to display the box of the box diagram, which is displayed by default.
·showfliers: Indicates whether to display outliers, which is displayed by default.
· labels : Indicates the label of the box plot.
·boxprops: A dictionary representing the properties of the control box.
Use the boxplot() function to draw a box plot, the code is as follows.
In [22]:
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(100)
#Draw a box plot : display the median line, the box width is 0.3 , fill the box color, and do not display outliers
plt.boxplot(data, meanline=True, widths=0.3, patch_artist=True,
         showfliers=False)
plt.show()
Run the program, the effect is shown in Figure 2-20.
2.8.2 Example 8: National electricity generation statistics in 2017 and 2018
The "China Report Hall" website has monitored and counted the power generation in 2017 and 2018, and the results are shown in Table 2-7
shown.
According to the data in Table 2-7, use the data in the two columns of "power generation (100 million kWh)" as the data on the x- axis, and use "2017
Year" and "2018" as the scale labels of the y- axis, use the boxplot() function to draw the national power generation in 2017 and 2018
Quantitative box plot, the specific code is as follows.
In [23]:
# 08_generation_capacity
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
data_2018 = np.array([5200, 5254.5, 5283.4, 5107.8, 5443.3, 5550.6,
              6400.2, 6404.9, 5483.1, 5330.2, 5543, 6199.9])
data_2017 = np.array([4605.2, 4710.3, 5168.9, 4767.2, 4947, 5203,
              6047.4, 5945.5, 5219.6, 5038.1, 5196.3, 5698.6])
# draw boxplot
plt.boxplot([data_2018, data_2017], labels=('2018 ' , '2017 ' ),
          meanline=True, widths=0.5, vert=False, patch_artist=True)
plt.show()
Run the program, the effect is shown in Figure 2-21.
In Figure 2-21, the x -axis represents power generation, the y -axis represents the year, the box represents the concentrated data range, and the vertical
The line represents the median, the vertical lines on the left and right edges of the box represent the minimum and maximum values, and the hollow circle to the right of the vertical line on the right edge represents
outliers. It can be seen from Figure 2-21 that most of the monthly power generation in 2017 was in the range of 480 billion to 530 billion kWh.
In 2018, the monthly power generation was mostly distributed in the range of 525 billion to 570 billion kWh.

Guess you like

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