pyplot只有两个数值做barplot

时间:2022-07-24
本文章向大家介绍pyplot只有两个数值做barplot,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

只计算出了两个比例值,想用这两个比例做出barplot,在Stack Overflow翻了好些帖子才找到怎么做:

import matplotlib.pyplot as plt
hit_near = sum(table[table['hit or non-hit']==1]['Near FANTOM enhancer']==1)/(table[table['hit or non-hit']==1].shape[0])
nonhit_near = sum(table[table['hit or non-hit']==0]['Near FANTOM enhancer']==1)/(table[table['hit or non-hit']==0].shape[0])

fig = plt.figure(figsize=(3,6))
barlist = plt.bar(['non-hit', 'hit'], [nonhit_near*100, hit_near*100], width=0.5, )
barlist[0].set_color('r')
barlist[1].set_color('blue')
plt.ylabel('%')
plt.title('Gene Body < 1kb from FANTOM Enhancer')
plt.show()

其中, plt.bar()中第一组list是横坐标,第二组是纵坐标; 宽度为0.5; 赋值给barlist变量,barlist[0].set_color给第一根柱子设置为红色;

如果想修改坐标刻度的话,可以:

from matplotlib.ticker import MultipleLocator
ymajorLocator = MultipleLocator(1)
ax=plt.gca()
ax.yaxis.set_major_locator(ymajorLocator)

这样可以把纵坐标修改为间隔为1.