首先,提供一种比较普遍的画法。
score10_15 = 0
score15_20 = 0
score20_25 = 0
score25_30 = 0
score30_35 = 0
score35_40 = 0
score40_45 = 0
score45_50 = 0
cell = ['1',' ','2',' ', '3',' ','4',' ','5']
pvalue =[score10_15,score15_20,score20_25,score25_30,score30_35,score35_40,score40_45,score45_50,0] #自己定义数据的大小,我是从txt文件里读并统计的,这里有些不一样
#print(score30_35+score35_40+score40_45+score45_50)
index = np.arange(len(cell))
figsize = (15,8)#调整绘制图片的比例
plt.bar(index, pvalue, width=1,color="chocolate",align='edge') #绘制柱状图
#plt.title(' ') #图像的名称
plt.xlim(1, 9)
def formatnum(x, pos):
return '$%.1f$x$10^{4}$' % (x/10000)
formatter = FuncFormatter(formatnum)
plt.gca().yaxis.set_major_formatter(formatter)
plt.xticks(index , cell,fontsize=15) #将横坐标用cell替换,fontsize用来调整字体的大小
plt.yticks(fontsize=15)
#plt.legend() #显示label
plt.savefig('your picture name.png',dpi = 400) #保存图像,dpi可以调整图像的像素大小
第一种画法呢,就是使用plt.gca().yaxis.set_major_formatter()函数来规范X轴和Y轴的数据。这样的画法比较简单,但是出来的图片并不是很美观。我们可以看一下结果。下图为我自己数据结果。
这样导致每个数字都跟着很长的后缀,看着有点不好。所以就想着怎么更改好看一点,就找到了现在的方法。
首先,你先给数据除上一个你要表示的数,比如我是要表示,所以我给数据除上10000。最后怎么把这个
表示出来呢?找寻了半天方法,只找到一个很笨的方法使用plt.text()。
例如此例,我是这么表示的。
plt.text(0, 1.95, r'x 10$^{4}$',fontsize=15)
解释下该函数,第一个为横坐标位置,第二个为纵坐标位置,第三个是咱要弄的具体文本。公式的话可以用latex格式表示。最终效果如下。
可以看到,经过更改后的图像美观多了。如果有什么好方法达到同样的效果,望告知。