2017-03-09 103 views
2
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着色只在matplotlib barplot某条不同颜色的

现在,我只想彩色B和H用不同的颜色可以说,红色和橙色。我会非常感激任何建议来实现这一目标。谢谢。

回答

1

试试这个:

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

+0

谢谢maxu ..做的目的:) –

+0

@Aceraceae,很高兴我能帮助:) – MaxU