0


python-matplotlib-箱线图为不同的箱体设置不同颜色

箱线图怎么为不同的箱体设置不同颜色?

方法:

f=df.boxplot(patch_artist=True,return_type='dict')# 这里共有四个box
color=['k','g','r','deepskyblue']# 有多少box就对应设置多少颜色for box,c inzip(f['boxes'], color):# 箱体边框颜色
    box.set( color=c, linewidth=2)# 箱体内部填充颜色
    box.set( facecolor = c )

实战:

%matplotlib notebook
fig, ax = plt.subplots(1,6,figsize=(9.5,2.5))

color=['r','g']
labels =["epilepsy","no_epilepsy"]

ax1 = plt.subplot(161)
f1 = ax1.boxplot(np.array([preprocessing.scale(np.mean(psd_e[:,:,:], axis=1)),preprocessing.scale(np.mean(psd_en[:,:,:], axis=1))]),showfliers=False,patch_artist=True,labels = labels)
plt.xticks([])
plt.ylabel('z-score')
plt.xlabel('A')for box,c inzip(f1['boxes'], color):# 箱体内部填充颜色
    box.set( facecolor = c )

ax2 = plt.subplot(162)
f2 = ax2.boxplot(np.array([preprocessing.scale(np.mean(pywt_energy_e[:,:,:], axis=1)),preprocessing.scale(np.mean(pywt_energy_en[:,:,:], axis=1))]),showfliers=False,patch_artist=True)
plt.xticks([])
plt.xlabel('B')for box,c inzip(f2['boxes'], color):# 箱体内部填充颜色
    box.set( facecolor = c )

ax3 = plt.subplot(163)
f3 = ax3.boxplot(np.array([preprocessing.scale(np.mean(pywt_energyRate_e[:,:,:], axis=1)),preprocessing.scale(np.mean(pywt_energyRate_en[:,:,:], axis=1))]),showfliers=False,patch_artist=True)
plt.xticks([])
plt.xlabel('C')for box,c inzip(f3['boxes'], color):# 箱体内部填充颜色
    box.set( facecolor = c )

ax4 = plt.subplot(164)
f4 = ax4.boxplot(np.array([preprocessing.scale(np.mean(pywt_entropy_e[:,:,:], axis=1)),preprocessing.scale(np.mean(pywt_entropy_en[:,:,:], axis=1))]),showfliers=False,patch_artist=True)
plt.xticks([])
plt.xlabel('D')for box,c inzip(f4['boxes'], color):# 箱体内部填充颜色
    box.set( facecolor = c )

ax5 = plt.subplot(165)
f5 = ax5.boxplot(np.array([preprocessing.scale(np.mean(peEntropy_e[:,:,0], axis=1)),preprocessing.scale(np.mean(peEntropy_en[:,:,0], axis=1))]),showfliers=False,patch_artist=True,labels = labels)
plt.xticks([])
plt.xlabel("E")for box,c inzip(f5['boxes'], color):# 箱体内部填充颜色
    box.set( facecolor = c )

ax6 = plt.subplot(166)
f6 = ax6.boxplot(np.array([preprocessing.scale(np.mean(shEntropy_e[:,:,0], axis=1)),preprocessing.scale(np.mean(shEntropy_en[:,:,0], axis=1))]),showfliers=False,patch_artist=True,labels = labels)
plt.xticks([])
plt.xlabel('F')for box,c inzip(f6['boxes'], color):# 箱体内部填充颜色
    box.set( facecolor = c )

plt.tight_layout()
plt.show()
plt.savefig("./minist.jpg")

请添加图片描述


本文转载自: https://blog.csdn.net/qq_37006625/article/details/127908633
版权归原作者 Stig_Q 所有, 如有侵权,请联系我们删除。

“python-matplotlib-箱线图为不同的箱体设置不同颜色”的评论:

还没有评论