熊猫酒吧图不列出颜色清单

发布于 2025-01-17 13:57:39 字数 1442 浏览 3 评论 0原文

我试图用不同的颜色为条形图着色,但是当我将颜色列表传递给 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()

输出

我尝试了很多方法来使其发挥作用,包括 如何在 matplotlib 条形图中添加颜色?, 在 matplotlib Python 中设置不同的条形颜色[重复]如何在Matplotlib中为条形图的条形设置不同的颜色?

I'm trying to colour a bar chart with different colours, but when I pass a list of colours to the color argument, it still colors all bars the same.

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()

Output

I have tried a number of things to make it work, including How to put colors in a matplotlib bar chart?, Setting Different Bar color in matplotlib Python [duplicate] and How to set Different Color(s) for Bars of Bar Plot in Matplotlib?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2025-01-24 13:57:39

如上所述, 按列设置颜色,您只有一个列,因此一种可能性是切换回Vanilla

如果您仍然想使用熊猫地块,请将您的标签转移到列中:

combi_df.pivot(columns='Labels', values='Number').plot.bar(color=c, stacked=True, ax=ax)

< img src =“ https://i.sstatic.net/yjnnw.png” width =“ 300”>

或使其水平并用yticklabels

combi_df.pivot(columns='Labels', values='Number').plot.barh(color=c, stacked=True, legend=False, ax=ax)
ax.set_yticklabels(combi_df['Labels'])

As commented, DataFrame.plot.bar sets colors by column and you only have one column, so one possibility is to switch back to the vanilla pyplot.bar.

If you still want to use a pandas plot, pivot your Labels into columns:

combi_df.pivot(columns='Labels', values='Number').plot.bar(color=c, stacked=True, ax=ax)

Or make it horizontal and replace the legend with yticklabels:

combi_df.pivot(columns='Labels', values='Number').plot.barh(color=c, stacked=True, legend=False, ax=ax)
ax.set_yticklabels(combi_df['Labels'])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文