python matplotlib notes: box plot

1、参数
matplotlib.pyplot.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, capwidths=None, *, data=None)

x Array or a sequence of vectors.
输入的数据. If a 2D array, a boxplot is drawn for each column in x. If a sequence of 1D arrays, a boxplot is drawn for each array in x.

notch: Boolean type, default: False
whether to draw a notch box plot (True) or a rectangular box plot (False). The notch represents the confidence interval (CI) around the median. The introduction of the bootstrap field describes how to calculate the position of the groove, but the position of the groove can also be overridden by setting the conf_intervals parameter.

sym:字符串类型, 可选操作
The default symbol for flier points. An empty string (‘’) hides the fliers. If None, then the fliers default to ‘b+’. More control is provided by the flierprops parameter.

vert: Boolean type, default: True.
If True, vertical boxes are drawn. If False, horizontal boxes are drawn.

whis: floating point number or a bunch of floating point numbers in the form: (float, float), default: 1.5
shadow line position.
If it is a floating point number, the lower shadow line is located at the lowest base point above Q1 - whis* (Q3-Q1) , the upper shadow line is located at the highest base point below Q3 + whis* (Q3-Q1), where Q1 and Q3 are the first and third quartiles respectively. The default value of whis = 1.5 is consistent with Tukey's original definition of a box plot.

If it is a pair of floating points, it represents the percentile of hatching (such as (5, 95)). In particular, setting it to (0, 100) causes the whiskers to cover the entire data range.

In the edge case of Q1 == Q3, if autorange is True, the hatch is automatically set to (0, 100) (covering the entire data range).

Data outside the hatch are considered outliers and plotted as a single point.

bootstrap:整数, optional
Specifies whether to bootstrap the confidence intervals around the median for notched boxplots. If bootstrap is None, no bootstrapping is performed, and notches are calculated using a Gaussian-based asymptotic approximation (see McGill, R., Tukey, J.W., and Larsen, W.A., 1978, and Kendall and Stuart, 1967). Otherwise, bootstrap specifies the number of times to bootstrap the median to determine its 95% confidence intervals. Values between 1000 and 10000 are recommended.

usermedians:1D array-like, optional
A 1D array-like of length len(x). Each entry that is not None forces the value of the median for the corresponding dataset. For entries that are None, the medians are computed by Matplotlib as normal.

conf_intervals:array-like, optional
A 2D array-like of shape (len(x), 2). Each entry that is not None forces the location of the corresponding notch (which is only drawn if notch is True). For entries that are None, the notches are computed by the method specified by the other parameters (e.g., bootstrap).

positions: array-like, optional
box position. Scale and limits are automatically set to match the location. The default is range(1,N+1), where N is the number of boxes to draw.

widths: Float or array
The width of the box. Defaults to 0.5, or 0.15 times (the distance between extreme positions) if the value is less than 0.5.

patch_artistbool, default: False
If it is False, Line2D artist is used to draw the box. Otherwise, Patch artists will be used to draw the box.

labelssequence, optional
Labels for each dataset (one per dataset).

manage_ticksbool, default: True
If True, the tick locations and labels will be adjusted to match the boxplot positions.

autorangebool, default: False
When True and the data are distributed such that the 25th and 75th percentiles are equal, whis is set to (0, 100) such that the whisker ends are at the minimum and maximum of the data.

meanlinebool, default: False
If True (and showmeans is True), will try to render the mean as a line spanning the full width of the box according to meanprops (see below). Not recommended if shownotches is also True. Otherwise, means will be shown as points.

zorderfloat, default: Line2D.zorder = 2
order of box plot

2. Sample

# 数据下载自 中国货币网
df = pd.read_excel("../data/回购定盘利率.xlsx")
df = df.iloc[7:-2]
# 设置图形的显示风格
fig = plt.figure(figsize=(12, 6))
plt.style.use('ggplot')
 
# 设置中文和负号正常显示
plt.rcParams['font.sans-serif'] = 'sans-serif'
plt.rcParams['axes.unicode_minus'] = False
 

# 设置y轴的范围
# plt.ylim(0,85)
list_arr = [df["FR001"].values,df["FR007"].values,
            df["FR014"].values,df["FDR001"].values,
           df["FDR007"].values,df["FDR014"].values]
# 绘图
plt.boxplot(x = list_arr, 
            patch_artist=True,
            # 添加具体的标签名称
            labels = ['FR001','FR007','FR014',"FDR001","FDR007","FDR014"], 
            showmeans=True, 
            boxprops = {
    
    'color':'black','facecolor':'#9999ff'}, 
            flierprops = {
    
    'marker':'o','markerfacecolor':'red','color':'black'},
            meanprops = {
    
    'marker':'D','markerfacecolor':'indianred'},
            medianprops = {
    
    'linestyle':'--','color':'orange'})
# 去除箱线图的上边框与右边框的刻度标签
plt.tick_params(top='off', right='off')

# 显示图形
plt.show()

Insert image description here

3. Refer to
matplotlib official website

Guess you like

Origin blog.csdn.net/weixin_39747882/article/details/128436686