更改 pandas 条形图的颜色 [英] Change colours of Pandas bar chart

查看:125
本文介绍了更改 pandas 条形图的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望熊猫图表中的条形被赋予不同的颜色.根据此帖子以及类似的内容.

I want the bars in a Pandas chart to each be given a different colour. The solution looks pretty simple according to this post and ones like it.

当我尝试模拟解决方案时,最终所有条形都使用相同的颜色(尽管与标准颜色不同).我想我做错了什么,但我看不出它是什么.还有其他人看到吗?

When I try to emulate the solution, I end up with all the bars the same colour (albeit a different colour from the standard one). I guess that I'm doing something wrong, but I can't see what it is. Anyone else see it?

fig = df.plot(kind='bar',    # Plot a bar chart
            legend=False,    # Turn the Legend off
            width=0.75,      # Set bar width as 75% of space available
            figsize=(8,5.8),  # Set size of plot in inches
            colormap='Paired')

colormap='Paired'是用于更改颜色的位.我得到这个:

The colormap='Paired' is the bit that's meant to change the colours. I get this:

很好,但是所有条形都是相同的颜色!如上所示,我正在对绘图进行其他更改,但它们都是文本格式或删除了轴详细信息.

It's nice, but all the bars are the same colour! I'm making other changes to the plot, as you can see above, but they're all formatting of text or removal of axis details.

推荐答案

让我们改用以下代码:

df.plot(kind='bar',    # Plot a bar chart
        legend=False,    # Turn the Legend off
        width=0.75,      # Set bar width as 75% of space available
        figsize=(8,5.8),  # Set size of plot in inches
        color=[plt.cm.Paired(np.arange(len(df)))])

这篇关于更改 pandas 条形图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆