Python绘制柱状图

import os
#输入想要存储图像的路径
os.chdir('路径')

import matplotlib.pyplot as plt 
import numpy as np 
#改变绘图风格
import seaborn as sns
sns.set(color_codes=True)


cell = ['HB','EVT','dS1','fFB1', 'fFB2', 'dM3',
        'dS2','Tcells']
pvalue = [0.234,0.808,0.71,0.084,
          0.451,0.754,0.139,0.944]


width = 0.20
index = np.arange(len(cell)) 
p1 = np.arange(0,len(cell),0.01)
p2 = 0.05 + p1*0

q1 = np.arange(0,len(cell),0.01)
q2 = 0.1 + p1*0

figsize = (10,8)#调整绘制图片的比例
plt.plot(p1,p2,color = 'red',label = '5% significance level')#绘制直线
plt.plot(q1,q2,color = 'yellow',label = '10% significance level')#绘制直线
#若是不想显示直线,可以直接将上面两行注释掉
plt.bar(index, pvalue, width,color="#87CEFA") #绘制柱状图
#plt.xlabel('cell type') #x轴
plt.ylabel('p value') #y轴
plt.title('Result of Network Screen') #图像的名称
plt.xticks(index, cell,fontsize=5) #将横坐标用cell替换,fontsize用来调整字体的大小
plt.legend() #显示label
plt.savefig('test.png',dpi = 400) #保存图像,dpi可以调整图像的像素大小

效果图:



test.png