仅在matplotlib条形图中使用不同颜色着色某些条形

时间:2017-03-09 11:24:14

标签: python pandas matplotlib

df=pd.DataFrame([770,215,179,107,83,82,70,60,57,54,52],index = ['A','B','C','D','E','F','G','H','I','J','K']) 

ax = df.plot(kind='bar',stacked = False,alpha=0.75, rot=45,fontsize=20)
ax.legend_.remove()
for p in ax.patches:
        ax.annotate(np.round(p.get_height(),decimals=0).astype(np.int64), (p.get_x()+p.get_width()/2., p.get_height()), ha='center', va='center', xytext=(2, 10), textcoords='offset points',fontsize=20)
plt.ylabel('y ',fontsize=25)
plt.title('x',fontsize=30)
plt.show()
plt.save()
plt.close()

给了我:enter image description here

现在,我想用不同颜色的颜色B和H来说红色和橙色。我要感谢任何有关实现这一目标的建议。谢谢。

1 个答案:

答案 0 :(得分:2)

试试这个:

ax.patches[df.index.get_indexer(['B'])[0]].set_facecolor('r')

ax.patches[df.index.get_indexer(['H'])[0]].set_facecolor('orange')

enter image description here