pythonMatplotlib中绘制多箱线图(boxplot)两种绘制⽅式,不可混淆
⼀、DataFrame绘制单坐标轴
两种都试⼀下    可能我的anaconda有点问题  2代码不好⽤      下次再完善
1、anaconda
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pylab import *
rcParams['axes.unicode_minus'] = False
rcParams['font.sans-serif'] = ['Simhei']
y1 = (1,2,3,4,5,6,7,8,10)
y2 = (1,2,3,10,5,6,7,8,15)
data = pd.DataFrame({"Seq2seq": y1, "LSTM": y2})
df = pd.DataFrame(data)
df.plot.box()
plt.xlabel("",fontsize=16)
plt.ylabel("",fontsize=16)
print(df.describe())#显⽰中位数、上下四分位数、标准偏差等内容
plt.show()
2、原始版本
import numpy as np
import pandas as pdmatplotlib中subplot
import matplotlib.pyplot as plt
from pylab import *
rcParams['axes.unicode_minus'] = False
rcParams['font.sans-serif'] = ['Simhei']
data = {"B"[1,2,3,4,5,6,7,8,10], "C"[1,2,3,10,5,6,7,8,15], "D"[1,8,3,4,5,6,7,8,12], "E"[1,13,3,4,5,6,11,8,9]} df = pd.DataFrame(data)
df.plot.box()
plt.xlabel("横轴",fontsize=16)
plt.ylabel('纵轴',fontsize=16) id(line, alpha=0.8)
print(df.describe())#显⽰中位数、上下四分位数、标准偏差等内容
plt.show()
结果如图:
⼆,subplot+boxplot多图显⽰
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pylab import *
rcParams['axes.unicode_minus'] = False
rcParams['font.sans-serif'] = ['Simhei']
data = {
'B': [1,2,3,4,5,6,7,8,10],
'C': [1,2,3,10,5,6,7,8,15],
'D': [1,8,3,4,5,6,7,8,12],
"E": [1,13,3,4,5,6,11,8,9]
}
box_1, box_2, box_3, box_4 = data['B'], data['C'], data['D'], data['E']
plt.subplot(221)
labels = 'B','C','D','E'#图例
plt.boxplot([box_1, box_2, box_3, box_4],notch=False, labels = labels,patch_artist = False, boxprops = {'color':'black','linewidth':'2.0'},
capprops={'color':'black','linewidth':'2.0'})
plt.xlabel("横轴",fontsize=16)
plt.ylabel('纵轴',fontsize=16)
data1 = {
'B': [1,2,3,4,5,6,7,8,10],
'C': [1,2,3,10,5,6,7,8,15],
'D': [1,8,3,4,5,6,7,8,12],
"E": [1,13,3,4,5,6,11,8,9]
}
box_1, box_2, box_3, box_4 = data1['B'], data1['C'], data1['D'], data1['E']
plt.subplot(222)
labels = 'B','C','D','E'#图例
plt.boxplot([box_1, box_2, box_3, box_4],notch=False, labels = labels,patch_artist = False, boxprops = {'color':'black','linewidth':'2.0'},
capprops={'color':'black','linewidth':'2.0'},medianprops={'color':'green','linewidth':'1.5'})
plt.xlabel("横轴2",fontsize=20)
data2 = {
'B': [1,2,3,4,5,6,7,8,10],
'C': [1,2,3,10,5,6,7,8,15],
'D': [1,8,3,4,5,6,7,8,12],
"E": [1,13,3,4,5,6,11,8,9]
}
box_1, box_2, box_3, box_4 = data2['B'], data2['C'], data2['D'], data2['E']
plt.subplot(223)
labels = 'B','C','D','E'#图例
plt.boxplot([box_1, box_2, box_3, box_4],notch=False, labels = labels,patch_artist = False, boxprops = {'color':'black','linewidth':'2.0'},
capprops={'color':'black','linewidth':'2.0'})
plt.xlabel("横轴",fontsize=16)
plt.ylabel('纵轴',fontsize=16)
data3 = {
'B': [1,2,3,4,5,6,7,8,10],
'C': [1,2,3,10,5,6,7,8,15],
'D': [1,8,3,4,5,6,7,8,12],
"E": [1,13,3,4,5,6,11,8,9]
}
box_1, box_2, box_3, box_4 = data3['B'], data3['C'], data3['D'], data3['E']
plt.subplot(224)
labels = 'B','C','D','E'#图例
plt.boxplot([box_1, box_2, box_3, box_4],notch=False, labels = labels,patch_artist = False, boxprops = {'color':'black','linewidth':'2.0'},
capprops={'color':'black','linewidth':'2.0'})
plt.xlabel("横轴",fontsize=20)
plt.show()
结果如图:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。