【发布时间】:2014-09-01 02:10:11
【问题描述】:
我目前正在绘制大量分类数据的堆叠条形图,并且只希望在图例中显示重要的物种(在 ~500 个中我希望显示 ~25 个)。有没有一种简单的方法可以做到这一点?以下是我的代码:
labels=['0','20','40','60','80','100','120']
ax1=subj1df.plot(kind='barh', stacked=True,legend=True,cmap='Paired', grid=False)
legend(ncol=2,loc=2, bbox_to_anchor=(1.05, 1), borderaxespad=0.)
label1=['Baseline','8h','24h','48h','96h','120h']
ax1.set_yticklabels(label1, fontdict=None, minor=False)
plt.title('Subject 1 Phyla',fontweight='bold')
plt.savefig('Subject1Phyla.eps', format='eps', dpi=1000)
ax1.set_xticklabels(labels)
编辑:尝试添加此项以仅显示一个图例条目,但仅返回一个空图例:
h, l = ax1.get_legend_handles_labels()
legend(l[4],h[4],ncol=2,loc=2, bbox_to_anchor=(1.05, 1), borderaxespad=0.)
【问题讨论】:
-
您的意思是要绘制所有 500 个变量,但在图例中只显示选定的 25 个?
标签: python matplotlib legend