我正在嘗試用不同的顏色為條形圖著色,但是當我將顏色串列傳遞給color引數時,它仍然為所有條形著色。
combi_df = pd.DataFrame(columns=['Labels', 'Number'])
label_list = ['one','two','three','four','five','six','seven','eight']
int_list = [302,11,73,10,68,36,42,30]
combi_df['Labels'] = label_list
combi_df['Number'] = int_list
fig = plt.figure()
ax = plt.subplot(111)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
c = ['#1b9e77', '#a9f971', '#fdaa48','#6890F0','#A890F0','#fdaa48','#6890F0','#A890F0']
combi_df[['Number']].plot(kind='bar',color=c ,legend=True, fontsize=10,ax=ax)
ax.legend(ax.patches, combi_df['Labels'], loc='upper center',bbox_to_anchor=(0.75, 1))
ax.set_xlabel("Labels", fontsize=12)
ax.set_ylabel("Number of occurences", fontsize=12)
plt.show()

我已經嘗試了很多方法來使它作業,包括
或使其水平并將圖例替換為yticklabels:
combi_df.pivot(columns='Labels', values='Number').plot.barh(color=c, stacked=True, legend=False, ax=ax)
ax.set_yticklabels(combi_df['Labels'])

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452283.html
標籤:Python 熊猫 matplotlib 阴谋 条形图
上一篇:如何洗掉線性散點圖上的例外值
