Matplotlib:如何使用pylab.bar()将误差条颜色与柱状图一致?
我想把我的误差条(error bars)涂成和柱状图一样的颜色。但是,根据我下面的代码,这并没有实现。
# Plot
x = sig['Glycan Name_Seal']
y_seal = sig['Average RFU_Seal']
yerr_seal = sig['StDev_Seal']
y_human = sig['Average RFU_Human']
yerr_human = sig['StDev_Human']
barwidth=0.5
alpha=1
plt.figure(0, figsize=(18,6))
plt.bar(left=sig.index,
color=['blue', 'blue'], height=y_seal, width=barwidth, yerr=yerr_seal, label='Seal', alpha=alpha)
plt.bar(left=sig.index + barwidth,
color=['red', 'red'], height=y_human, width=barwidth, yerr=yerr_human, label='Human', alpha=alpha)
plt.ylim(0,2500)
plt.ylabel('Average RFU', fontsize=14)
plt.xlabel('Glycan Number', fontsize=14)
plt.xlim(0, 56)
plt.title('Pre-Complexed p-value < 0.01', fontsize=18)
plt.legend(fontsize=16)
# Add a vertical line to distinguish alpha(2,3) and alpha(2,6)
plt.plot([1, 1], [0, 2500], color="black", linewidth=2)
plt.plot([9, 9], [0, 2500], color="black", linewidth=2)
plt.plot([11, 11], [0, 2500], color="black", linewidth=2)
# Add the texts greek alpha(2,3) and alpha(2,6)
plt.annotate(r"$\alpha$(2,6)", (3.5, 2250), fontsize=14)
plt.annotate(r"$\alpha$(2,3)", (29, 2250), fontsize=14)
plt.annotate("asialoglycan", xy=(0.25, 1950), xytext=(2, 1950), arrowprops=dict(arrowstyle="->", linewidth=2), fontsize=14)
plt.annotate(r"$\alpha$(2,3) and $\alpha$(2,6)", xy=(10, 1850), xytext=(12, 1850), arrowprops=dict(arrowstyle="->", linewidth=2), fontsize=14)
plt.savefig('Average RFU Plot.pdf')
结果是,Seal
的数据用蓝色的柱子和蓝色的误差条显示,而Human
的数据则是红色的柱子和绿色的误差条。
有没有办法让Human
的数据也显示成红色的误差条呢?
1 个回答
2
试着在出问题的图表中设置 ecolor='red'
。我建议你在第一个图表中也设置 ecolor='blue'
,因为看起来你那里只是因为是第一个颜色,所以错误条是蓝色的。更多细节可以查看 plt.bar 的文档。
这个设置是在调用 plt.bar
时完成的,如下所示:
plt.bar(..., ecolor='red', ...)