我正在使用matplotlib绘制条形图,当我试图访问标签(在X和Y轴上)以更改它们时,遇到了一个问题。特别是,本规范:
fig = plot.figure(figsize=(16,12), dpi=(300))
ax1 = fig.add_subplot(111)
ax1.set_ylabel("simulated quantity")
ax1.set_xlabel("simulated peptides - from most to least abundant")
# create the bars, and set a different color for the bars referring to experimental peptides
barlist = ax1.bar( numpy.arange(len(quantities)), [numpy.log10(x) for x in quantities] )
for index, peptide in enumerate(peptides) :
if peptide in experimentalPeptidesP or peptide in experimentalPeptidesUP :
barlist[index].set_color('b')
labelsY = ax1.get_yticklabels(which='both')
print "These are the label objects on the Y axis:", labelsY
print "These are the labels on the Y axis:", [item.get_text() for item in ax1.get_xticklabels( which='both')]
for label in labelsY : label.set_text("AAAAA")
ax1.set_yticklabels(labelsY)
给出以下输出:
^{pr2}$根据要求,结果图将“AAAAA”作为Y轴上每个标签的文本。我的问题是,虽然我能够正确地设置标签,但显然我无法获得它们的文本……文本应该存在,因为如果我不将标签替换为“aaaaaa”,我会得到下图:
如您所见,Y轴上有标签,我需要“获取”它们的文本。错误在哪里?在
提前谢谢你的帮助。在
编辑:多亏了迈克·穆勒的回答,我成功地做到了。显然,在我的例子中,仅仅调用draw()是不够的,我必须在使用savefig()保存图形之后获取值。这可能取决于matplotlib的版本,我运行的是1.5.1,Mike运行的是1.5.0。我还将研究一下functformatter,如下tcaswell建议的那样
您需要先渲染绘图才能真正获得标签。添加
draw()
工作:没有:
^{pr2}$使用
draw()
:相关问题 更多 >
编程相关推荐