pyplot只有两个数值做barplot

2020-08-31 10:05:26 浏览数 (1)

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

代码语言:javascript复制
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给第一根柱子设置为红色;

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

代码语言:javascript复制
from matplotlib.ticker import MultipleLocator
ymajorLocator = MultipleLocator(1)
ax=plt.gca()
ax.yaxis.set_major_locator(ymajorLocator)

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

0 人点赞