python 实现箱线图
1.导入需要的函数
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
2.读取数据
'读取npy数据'
score_A=np.load('C:\\Users\\DengBY\\Desktop\\P_A_LASSO\\score.npy')
score_D=np.load('C:\\Users\\DengBY\\Desktop\\P_D_LASSO\\score.npy')
score_N=np.load('C:\\Users\\DengBY\\Desktop\\P_N_LASSO\\score.npy')
score_V=np.load('C:\\Users\\DengBY\\Desktop\\P_V_LASSO\\score.npy')
score得到的数据类型为array类例如
[0.7826087 0.80434783 0.84782609 0.80434783 0.73913043 0.86956522
0.82608696 0.80434783 0.84782609 0.82608696 0.89130435 0.82608696
0.82608696 0.7173913 0.7826087 0.84782609 0.86956522 0.84782609]
3.创建一个和score等大小的空Dataframe
def dataframe():
a=[]
for i in range(500):
a.append(i)
b = [str(j) for j in a]
df_empty = pd.DataFrame(columns=b)
return df_empty
df_empty=dataframe()
4.将array中的值赋在空的dataframe中
df_empty.loc[len(df_empty)] =score_A
df_empty.loc[len(df_empty)] = score_D
df_empty.loc[len(df_empty)] = score_N
df_empty.loc[len(df_empty)] = score_V
5.命名箱线图
df_empty.rename(index={0:'A',1:'D',2:'N',3:'V'},inplace=True)
6.画图
df = pd.DataFrame(df_empty.values.T, index=df_empty.columns, columns=df_empty.index)
df.boxplot()
plt.title('BOX-plot-score')
plt.show()
7.得到箱线图的具体参数
print (df.describe())
8.结果