| 
 
	积分5344贡献 精华在线时间 小时注册时间2012-8-28最后登录1970-1-1 
 | 
 
 发表于 2020-5-7 18:18:24
|
显示全部楼层 
| 本帖最后由 Masterpiece 于 2020-5-10 22:48 编辑 
 复制代码import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6,5))
np.random.seed(1234)
data1 = np.random.gumbel(3, 4, 70)
data2= np.random.gumbel(3, 4, 80)
data3 = np.random.gumbel(3, 4, 90)
data4 = np.random.gumbel(3, 4, 100)
data = [data1, data2, data3, data4]
ax.boxplot(data,0,'rx',widths=0.2)
for i,dd in enumerate(data,start=1):
    ax.scatter(i,np.average(dd),
         color='g', marker='*',s=50)
ax.set_xticklabels(["data1", "data2", "data3", "data4"])
#新建小图用于添加图注
legend_data=np.array([-20,0,10,20,35,45,60,80,90,100])
ax_legend = plt.axes([0.86, 0.15, 0.2, 0.5])
ax_legend.boxplot(legend_data,0,'rx',widths=0.2)
ax_legend.scatter(1, 60,color='g', marker='*',s=50,label='Mean')
ax_legend.scatter(1, 120,color='r', marker='x',s=30,label='Flier')
q1=np.percentile(legend_data,25)
q3=np.percentile(legend_data,75)
ax_legend.text(1.12,np.median(legend_data),'Median',
               verticalalignment='center',
               horizontalalignment='left')
ax_legend.text(1.12,q3,'75%',
               verticalalignment='center',
               horizontalalignment='left')
ax_legend.text(1.12,q1,'25%',
               verticalalignment='center',
               horizontalalignment='left')
ax_legend.text(1.12,np.max(legend_data),'Max',
               verticalalignment='center',
               horizontalalignment='left')
ax_legend.text(1.12,np.min(legend_data),'Min',
               verticalalignment='center',
               horizontalalignment='left')
ax_legend.legend(bbox_to_anchor=(0.92,1.25),handlelength=0.7,
                bbox_transform=ax_legend.transAxes)
plt.axis('off')
plt.savefig('box_legend.png',dpi=200,bbox_inches='tight')
plt.show()
  
 | 
 |