如何更改matplotlib boxplots中平均点的厚度

2024-06-06 14:22:25 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我使用matplotlib制作箱线图的代码

for i in range(4):
    data = np.random.normal(0, i+1, (100,9))
    positions = np.arange(9) * (4 + 3) + i
    color = colors[i]
    
    # Specifications
    meanprops = {'marker':'o',
                'markerfacecolor':color,
                'markeredgecolor':'black',
                'markersize':3.0,
                'linewidth':0.5}
    medianprops = {'linestyle':'-',
                   'linewidth':0.5,
                   'color':'black'}
    boxprops = {'facecolor':color,
                'color':'black',
                'linewidth':0.5}
    flierprops = {'marker':'o',
                  'markerfacecolor':'black',
                  'markersize':1,
                  'markeredgecolor':'black'}
    whiskerprops = {'linewidth':0.5}
    capprops = {'linewidth':0.5}

    # Plot it
    _ = plt.boxplot(
                    data, 
                    positions=positions, 
                    widths=0.7,#(1, 1, 1),
                    whis=(0,100),               # Percentiles for whiskers
                    showmeans=True,             # Show means in addition to median
                    patch_artist=True,          # Fill with color
                    meanprops=meanprops,        # Customize mean points
                    medianprops=medianprops,    # Customize median points
                    boxprops=boxprops,
                    showfliers=False,            # Show/hide points beyond whiskers            
                    flierprops=flierprops,
                    whiskerprops=whiskerprops,
                    capprops=capprops
                    )

# Save it 
plt.xticks([])
plt.savefig('boxplot.png', dpi=600)

尽管我在meanprops中指定了'linewidth':0.5,但这并不影响平均点的厚度,而且它们比中间线厚得多

如何更改平均点的厚度?enter image description here


Tags: infordatapltpointscolorblackpositions