堆积直方图
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
%pylab
'''
绘图数据
'''
labels=['AD1','A2','D5',]
know_FLNC=np.array([13865,13577,10214])
novel_FLNC=np.array([162645,133663,105283])
unrecongnized_FLNC=np.array([23223,16170,14798])
Other=[41462,42131,22854]
##生成分组的直方图
fig,ax=plt.subplots(figsize=(5,8)) ##设置图片长宽
barwidth=0.5
#plt.style.use('ggplot')
with plt.style.context('ggplot'):
ax.bar(labels,novel_FLNC,label="novel_FLNC",width=barwidth)
ax.bar(labels,know_FLNC,label="know_FLNC",bottom=novel_FLNC,width=barwidth)
ax.bar(labels,unrecongnized_FLNC,label="unrecongnized_FLNC",bottom=novel_FLNC+know_FLNC,width=barwidth)
ax.bar(labels,Other,label="Other",bottom=novel_FLNC+know_FLNC+unrecongnized_FLNC,width=barwidth)
ax.legend()
####生成百分比的堆积直方图
fig,ax=plt.subplots(figsize=(5,8)) ##设置图片长宽
total=novel_FLNC+know_FLNC+unrecongnized_FLNC+Other ##每个分类中总FLNC数目
know_FLNC=np.true_divide(know_FLNC,total)
novel_FLNC=np.true_divide(novel_FLNC,total)
Other=np.true_divide(Other,total)
unrecongnized_FLNC=np.true_divide(unrecongnized_FLNC,total)
with plt.style.context('ggplot'):
ax.bar(labels,novel_FLNC,
label="novel_FLNC",
width=barwidth,
color="#E973A0"
)
ax.bar(labels,know_FLNC,
label="know_FLNC",
bottom=novel_FLNC,
width=barwidth,
color="#54C4D8"
)
ax.bar(labels,
unrecongnized_FLNC,
label="unrecongnized_FLNC",
bottom=novel_FLNC+know_FLNC,
width=barwidth,
color="#F8DA63"
)
ax.bar(labels,Other,label="Other",
bottom=novel_FLNC+know_FLNC+unrecongnized_FLNC,
width=barwidth,
color="#C1A954"
)
###隐藏坐标轴
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
##设置坐标轴字体大小
ax.set_xticklabels(labels,size="15")
ax.legend(loc="center",bbox_to_anchor=(0.5,1.05),ncol=4,fontsize='10') ##修改图例
fig.savefig('test.pdf', dpi=150, bbox_inches="tight")
Last updated