Matplotlib:如何使用pylab.bar()将错误栏的颜色设置为与bar相同的颜色?

时间:2014-06-09 22:29:13

标签: python matplotlib

我希望能够将我的错误栏颜色与条形颜色相同。但是,根据我的代码,这不会发生。

# 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 个答案:

答案 0 :(得分:2)

尝试在违规情节中设置ecolor='red'。我建议在你的第一个情节中设置ecolor='blue',因为看起来你只是在那里得到蓝色误差条,因为这是它们循环的第一种颜色。有关详细信息,请参阅the documentation for plt.bar

这是在plt.bar的调用中完成的:

plt.bar(..., ecolor='red', ...)