matplotlib-箱线图

箱线图

箱形图(Box-plot)又称为盒须图、盒式图或箱线图,一种用作显示一组数据分散情况资料的统计图。它能显示出一组数据的上限下限中位数上下四分位数,异常值。: 

四分位数,就是把组中所有数据由小到大排列并分成四等份,处于三个分割点位置的数字就是四分位数。

第一四分位数(Q1):又称“较小四分位数”或“下四分位数”,等于该样本中所有数值由小到大排列后第25%的数字。

Q1的位置 = (n+1)/4

第二四分位数(Q2):又称中位数,等于该样本中所有数值由小到大排列后第50%的数字。

Q2的位置=n+1/2

第三四分位数(Q3),又称较大四分位数或“上四分位数”,等于该样本中所有数值由小到大排列后第75%的数字。

Q3的位置=3(n+1)/4

四分位间距(InterQuartile Range,IQR):第三四分位数与第一四分位数的差距(Q3数据-Q1数据)。

Whiske上限:Q3数据+ 1.5*IQR 内的数据 , (1.5相当于系数,表示超过的比例,可调)

Whisker下限:Q1数据- 1.5*IQR 内的数据

"""
箱线图,
boxplot
x,y:dataframe中的列名(str)或者矢量数据
data:dataframe或者数组.
hue(str):dataframe的列名,按照列名中的值分类形成分类的条形图
palette:调色板,控制图像的色调
notch:表示箱线图中位数位置是否缩进,默认为False 不缩进
fliersize:float:用于指示异常值观察的标记大小
width:float:控制箱型图的宽度
linewidth:表示四分位数据线条粗细
"""

# 导入库
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\\WINDOWS\\Fonts\\msyhbd.ttf", size=8)

"""
the method of bad
"""
# 导入数据文件
csv_data = pd.read_csv('.\data\\ly_hon_bad_py.csv')
# 定义 figure
fig= plt.figure(1,figsize=(4.15,2))
fig.subplots_adjust(left=0.11, bottom=0.19, right=0.99, top=0.91, wspace=0.06, hspace=0.30)
# 绘制箱线图,数据是按照 boxplot 格式要求提前构造
ax=sns.boxplot(data=csv_data,x="num", y="data", hue="char", palette="dark",notch=True,fliersize=5,linewidth=0.5)

# marks
x_position=[0,1,2,9,11,12,13]#np.arange(1,13,1)
y_position=[1.15,1.15,1.15,1.15,1.15,1.15,1.15]
# 用散点图方法,添加 mark ,
plt.scatter(x_position,y_position,marker = '+', s = 50, color = 'b')#, color = 'r'
plt.ylim(-0.1,1.5)

#axes_add
ax.set_title('The common WE method',fontproperties=font)#the title of bad_methods
#ax.set_ylabel('$Mean_{WE}^{P3}$',fontproperties=font2)
ax.set_ylabel('')
plt.text(-2.2,0.75,'$Mean_{WE}^{P3}$',fontsize=10,rotation=90)
ax.set_xlabel('Electrodes',fontproperties=font)
elec2=['O2','O1','OZ','PZ','P4','P3','C4','CZ','C3','FZ','F4','F3','FP2','FP1'];
plt.yticks(fontsize=9)
ax.set_xticklabels(elec2,fontsize=9)
plt.legend(title="",loc='upper center',ncol=2,fontsize=9,frameon=False)#, frameon = False  ('Innocent','Guilty','chen','ran'),loc = (.85,.1),
plt.savefig(".\\figure_png\\bad_method_0525" + '.png', dpi=600)  # +elec2[num-1]  #循环保存图片  bad_method

"""
the method of good
"""
# 导入数据
csv_data = pd.read_csv('.\data\\ly_hon_good_py.csv')
fig= plt.figure(2,figsize=(4.15,2))
fig.subplots_adjust(left=0.09, bottom=0.19, right=0.99, top=0.91, wspace=0.06, hspace=0.30)
ax=sns.boxplot(x="num", y="data", hue="char", palette="dark",notch=True,data=csv_data,linewidth=1.5)#,orient="h",color=('#FF851B','#FF4136')
#marks
x_position=[0,1,2,12,13]#np.arange(1,13,1)
y_position=[1.25,1.25,1.25,1.25,1.25]
plt.scatter(x_position,y_position,marker = '+', s = 80, color = 'b')#, color = 'r'
plt.ylim(-0.1,1.5)
#axes_add
ax.set_title('The improved WE method',fontproperties=font)#the title of good_methods
#ax.set_ylabel('$Impv_{WE}^{P3}$',fontproperties=font2)
ax.set_ylabel('')
plt.text(-1.9,0.75,'$Impv_{WE}^{P3}$',fontsize=10,rotation=90)
ax.set_xlabel('Electrodes',fontproperties=font)
elec2=['O2','O1','OZ','PZ','P4','P3','C4','CZ','C3','FZ','F4','F3','FP2','FP1'];
plt.yticks(fontsize=6)
colors=[]
ax.set_xticklabels(elec2,fontsize=9)
plt.legend(title="",loc='upper center',ncol=2,fontsize=9,frameon=False)
plt.savefig(".\\figure_png\good_method_0525" + '.png', dpi=600)  # +elec2[num-1]  #循环保存图片  good_method
# plt.show()

"""
the difference of the mean of bad and good,
"""
# 导入数据
csv_data = pd.read_csv('.\data\\mean_dif_py.csv')
fig= plt.figure(3,figsize=(4.15,2))
ax=fig.add_subplot(111)
ax=fig.subplots_adjust(left=0.095, bottom=0.19, right=0.99, top=0.91, wspace=0.06, hspace=0.30)
bad_mean=csv_data.iloc[:14,1]
good_mean=csv_data.iloc[14:28,1]
# 绘图
m=0.3
x=np.arange(14)+1
plt.bar(x,bad_mean,width=m,color="#FF851B",edgecolor='k',linewidth=2)#yellowgreen  tomato,yerr=i_err_bad,ecolor='blue'
plt.bar(x+m,good_mean,width=m,color="#55A868",edgecolor='k',linewidth=2)#yerr=i_err_good,ecolor='orangered'
#yerr,均方误差数据,ecolor,均方误差的线条颜色
plt.legend(['The common WE method','The improved WE method'],fontsize=8, loc='upper right',frameon=False)
plt.plot([1,14],[0,0],linestyle='--',linewidth=1.0,color='k')
plt.title('The comparison of the difference of mean',fontproperties=font)
# 定坐标,添加 txt 文本
plt.text(-1.6,0.25,'Difference of mean',fontproperties=font,rotation=90)
plt.xlabel('Electrodes',fontproperties=font)
plt.xlim(0,15.8)
plt.yticks(fontsize=8)
elec2=['O2','O1','OZ','PZ','P4','P3','C4','CZ','C3','FZ','F4','F3','FP2','FP1']
xtick_x=x+m+0.04
plt.xticks(xtick_x,elec2,fontsize=9)#('Innocent','Guilty'),,fontproperties='Times New Roman'
plt.savefig(".\\figure_png\mean_diff_0525" + '.png', dpi=600)  # +elec2[num-1]  #循环保存图片  good_method
plt.show()
 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值