dibujar histograma de python

import matplotlib.pyplot as plt
import numpy as np
from pylab import *  # 支持中文
mpl.rcParams['font.sans-serif'] = ['Times New Roman']
mpl.rcParams['font.sans-serif'] = ['STSong']
import matplotlib.pyplot as plt
import numpy as np



plt.figure(figsize=(10,5),dpi=450)#画布大小

# plt.rcParams['figure.figsize']=(6.5,5)#绘图框大小
x = [u'  1','  2','  3']# 分类轴显示标签

saleA = [88.97,92.37,88.60]
saleB = [89.82, 87.59,91.43]
saleC = [90.96, 90.90,91.48]
saleD = [92.37, 90.60,92.90]
saleE = [94.37, 93.28,95.99]
saleF = [95.96, 93.28,93.90]
saleG = [94.83, 91.73,96.79]
saleH = [96.83, 93.56,96.99]
index = np.arange(3)
barW = 0.1  # barW控制条形宽度

# fig.add_subplot(121)
plt.bar(index, saleA, barW, color='#7e728c', label='11')
plt.bar(index + barW, saleB, barW, color='#A4EED4', label='22')
plt.bar(index+ 2*barW, saleC, barW, color='#A2D0F0', label='33')
plt.bar(index + 3*barW, saleD, barW, color='#61C82E', label='44')
plt.bar(index+ 4*barW, saleE, barW, color='#FBC071', label='55')
plt.bar(index + 5*barW, saleF, barW, color='#BB9BD1', label='66')
plt.bar(index+ 6*barW, saleG, barW, color='#8888E4', label='77')
plt.bar(index + 7*barW, saleH, barW, color='r', label='my')


# 给柱子标数字
# 第一组saleA柱子
plt.text(index[0], saleA[0] + 0.2, saleA[0], ha='center',fontproperties='Times New Roman',  fontsize=9,  zorder=10)
plt.text(index[1], saleA[1] + 0.2, saleA[1], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
plt.text(index[2], saleA[2] + 0.2, saleA[2], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
# 第二组saleB柱子
plt.text(index[0]+ barW, saleB[0] + 0.2, saleB[0], ha='center',fontproperties='Times New Roman',  fontsize=9,  zorder=10)
plt.text(index[1]+ barW, saleB[1] + 0.2, saleB[1], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
plt.text(index[2]+ barW, saleB[2] + 0.2, saleB[2], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
# 第3组saleC柱子
plt.text(index[0]+ 2*barW, saleC[0] + 0.2, saleC[0], ha='center',fontproperties='Times New Roman',  fontsize=9,  zorder=10)
plt.text(index[1]+ 2*barW, saleC[1] + 0.2, saleC[1], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
plt.text(index[2]+ 2*barW, saleC[2] + 0.2, saleC[2], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
# 第4组saleD柱子
plt.text(index[0]+ 3*barW, saleD[0] + 0.2, saleD[0], ha='center',fontproperties='Times New Roman',  fontsize=9,  zorder=10)
plt.text(index[1]+ 3*barW, saleD[1] + 0.2, saleD[1], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
plt.text(index[2]+ 3*barW, saleD[2] + 0.2, saleD[2], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
# 第5组saleE柱子
plt.text(index[0]+ 4*barW, saleE[0] + 0.2, saleE[0], ha='center',fontproperties='Times New Roman',  fontsize=9,  zorder=10)
plt.text(index[1]+ 4*barW, saleE[1] + 0.2, saleE[1], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
plt.text(index[2]+ 4*barW, saleE[2] + 0.2, saleE[2], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
# 第6组saleF柱子
plt.text(index[0]+ 5*barW, saleF[0] + 0.2, saleF[0], ha='center',fontproperties='Times New Roman',  fontsize=9,  zorder=10)
plt.text(index[1]+ 5*barW, saleF[1] + 0.2, saleF[1], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
plt.text(index[2]+ 5*barW, saleF[2] + 0.2, saleF[2], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
# 第7组saleG柱子
plt.text(index[0]+ 6*barW, saleG[0] + 0.2, saleG[0], ha='center',fontproperties='Times New Roman',  fontsize=9,  zorder=10)
plt.text(index[1]+ 6*barW, saleG[1] + 0.2, saleG[1], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
plt.text(index[2]+ 6*barW, saleG[2] + 0.2, saleG[2], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
# 第8组saleH柱子
plt.text(index[0]+ 7*barW, saleH[0] + 0.2, saleH[0], ha='center',fontproperties='Times New Roman',  fontsize=9,  zorder=10)
plt.text(index[1]+ 7*barW, saleH[1] + 0.2, saleH[1], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)
plt.text(index[2]+ 7*barW, saleH[2] + 0.2, saleH[2], ha='center', fontproperties='Times New Roman', fontsize=9,  zorder=10)



# plt.xlim([-5,100]) #横坐标范围
plt.ylim([0,119])  # 横坐标范围
# plt.title("并列柱状图")
# plt.xticks(fontproperties = 'Times New Roman', size = 14)
plt.yticks(fontproperties = 'Times New Roman', size = 14)

plt.ylabel("xxx/%", rotation=90,size=22)
plt.xticks(index+ 3.5*barW, x,size=22)
plt.legend(ncol=4,loc='upper left',prop={'size':14},frameon=False)
# 
plt.tick_params(direction='in')  # 刻度向里

plt.rcParams['savefig.dpi'] = 600 #图片像素
plt.rcParams['figure.dpi'] =600 #分辨率

# plt.xticks([])#不显示横坐标刻度
# plt.xlabel(loc=None,u"1                                                              3                                                                             2")  # X轴标签
# plt.xlabel(loc=None)
plt.savefig('zhu.jpg', dpi=450,bbox_inches='tight')

plt.show()

 

Supongo que te gusta

Origin blog.csdn.net/m0_63172128/article/details/130486248
Recomendado
Clasificación