- Function function: reflect the abnormal situation of the data; it is mainly used to analyze the internal distribution state or dispersion state of the data, including upper and lower limits, quantiles, and outliers;
- 调用方法:plt.boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=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)
- Parameter Description:
- x: Specify the data to draw the boxplot;
- notch: Whether to display the boxplot in the form of a notch, the default is not notch;
- sym: Specifies the shape of the abnormal point, the default is 'o';
- vert: Whether to place the boxplot vertically, the default is to place vertically;
- whis: Specify the distance between the upper and lower whiskers and the upper and lower quartiles, the default is 1.5 times the quartile difference;
- positions: Specify the position of the boxplot, the default is [0,1,2…];
- widths: Specify the width of the boxplot, the default is 0.5;
- patch_artist: Whether to fill the color of the box;
- meanline: Whether to represent the mean in the form of a line, and the default is to represent it in points;
- showmeans: Whether to display the mean value or not by default;
- showcaps: Whether to display the two lines at the top and end of the boxplot, which are displayed by default;
- showbox: Whether to display the box of the box plot, the default display;
- showfliers: whether to display abnormal values, the default display;
- boxprops: Set the properties of the box, such as border color, fill color, etc.;
- boxprops = {'color':'g', 'facecolor':'yellow'}
- 'color' : 'g' the color of the box border
- 'facecolor' : 'yellow' box fill color
- labels: Add labels to the boxplot, similar to the role of the legend;
- flierprops: Set the properties of outliers, such as the shape, size, fill color, etc. of outliers;
- medianprops: Set the properties of the median, such as line type, thickness, etc.;
- meanprops: Set the properties of the mean, such as point size, color, etc.;
- capprops: Set the properties of the top and end lines of the boxplot, such as color, thickness, etc.;
- whiskerprops: Set whisker properties, such as color, thickness, line type, etc.;
Draw a boxplot:
#利用numpy库生成100个服从标准正态分布随机数
x = np.random.normal(0,1,100)
plt.boxplot(x, # 指定绘图数据
notch = True,sym = 'o' , # 凹凸形状
patch_artist = True, # 要求用自定义颜色填充盒形图,默认白色填充
showmeans = True, # 以点的形式显示均值
boxprops = {'color':'black', 'facecolor':'#9999ff'}, # 设置箱体属性,填充色和边框色
flierprops = {'marker':'o', 'markerfacecolor':'red', 'color':'black'}, # 设置异常值属性,点的形状、填充色和边框色
meanprops = {'marker':'o', 'markerfacecolor':'c',}, # 设置均值点的属性,点的形状、填充色
medianprops = {'linestyle':'--', 'color':'orange'}) # 设置中位数线的属性,线的类型和颜色
# 去除箱线图的上边框与右边框的刻度标签
plt.tick_params(top = 'off', right = 'off')
plt.show()
Draw multiple sets of boxplots: observe the distribution and outliers of multiple sets of data (or features) at the same time
#利用 numpy库生成三组正态分布随机数
x = [np.random.normal(0,std,100) for std in range(1,4)]
# 绘图
plt.boxplot(x,
patch_artist = True,sym = 'o',
labels = ['一组','二组','三组'], # 添加具体的标签名称
showmeans=True,
boxprops = {'color':'black','facecolor':'#9999ff'},
flierprops = {'marker':'o','markerfacecolor':'red','color':'black'},
meanprops = {'marker':'D','markerfacecolor':'indianred','color' : 'y',},
medianprops = {'linestyle':'--','color':'orange'})
# 显示图形
plt.show()