效果图
获取箱线图特征值代码
class Tool:
def boxFeature(self,bp):
"""获取箱体图特征
【输入】 bp boxplot返回的字典对象
【输出】 llim, q1, q2, q3, ulim
"""
(x_q1, y_q1), (x_llim, y_ulim) = bp['whiskers'][0].get_xydata() # 下四分位数
(x_q3, y_q3), (x_ulim, y_llim) = bp['whiskers'][1].get_xydata() # 上四分位数
(x_left_low, y_llim), (x_right_up, y_llim) = bp['caps'][0].get_xydata() # 下限 非异常范围内的最小值
(x_left_up, y_ulim), (x_right_low, y_ulim) = bp['caps'][1].get_xydata() # 上限 非异常范围内的最大值
(x_left_median, y_q2), (x_right_median, y_q2) = bp['medians'][0].get_xydata() # 中位数
return y_llim, y_q1, y_q2, y_q3, y_ulim
绘制箱线图代码
rain_cins, unrain_cins, = rainSet['cins'], unrainSet['cins'].dropna()
tool = Tool()
fig, ax = plt.subplots(nrows=1, ncols=2)
#labels must be a list,or going wrong
bp1= ax[0].boxplot(rain_cins,labels = ['rain'],sym='r')
ax[0].set_title('cins of boxplot', fontsize=20) # 标题,并设定字号大小
bp2= ax[1].boxplot(unrain_cins, labels=['unrain'], sym='r')
ax[1].set_title('cins of boxplot', fontsize=20)
# 获取特征值
llim_ax0, q1_ax0, q2_ax0, q3_ax0, ulim_ax0=tool.boxFeature(bp1)
llim_ax1, q1_ax1, q2_ax1, q3_ax1, ulim_ax1=tool.boxFeature(bp2)
# ax0添加标签
ax[0].text(1.2, llim_ax0, '%.2f' % llim_ax0, verticalalignment='center', fontsize=10, backgroundcolor="white")
ax[0].text(1.2, q1_ax0, '%.2f' % q1_ax0, verticalalignment='center', fontsize=10, backgroundcolor="white")
ax[0].text(1.2, q2_ax0, '%.2f' % q2_ax0, verticalalignment='center', fontsize=10, backgroundcolor="white")
ax[0].text(1.2, q3_ax0, '%.2f' % q3_ax0, verticalalignment='center', fontsize=10, backgroundcolor="white")
ax[0].text(1.2, ulim_ax0, '%.2f' % ulim_ax0, verticalalignment='center', fontsize=10, backgroundcolor="white")
# ax1添加标签
ax[1].text(1.2, llim_ax1, '%.2f' % llim_ax1, verticalalignment='center', fontsize=10, backgroundcolor="white")
ax[1].text(1.2, q1_ax1, '%.2f' % q1_ax1, verticalalignment='center', fontsize=10, backgroundcolor="white")
ax[1].text(1.2, q2_ax1-1, '%.2f' % q2_ax1, verticalalignment='center', fontsize=10, backgroundcolor="white")
ax[1].text(1.2, q3_ax1+0.5, '%.2f' % q3_ax1, verticalalignment='center', fontsize=10, backgroundcolor="white")
ax[1].text(1.2, ulim_ax1, '%.2f' % ulim_ax1, verticalalignment='center', fontsize=10, backgroundcolor="white")
# 显示图像
plt.show()