更改熊猫条形图的颜色

use*_*331 3 plot matplotlib pandas

我希望在熊猫图表中为每个柱赋予不同的颜色。根据这篇文章和类似的文章,该解决方案看起来非常简单。

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

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')
Run Code Online (Sandbox Code Playgroud)

colormap='Paired'是要更改颜色的位。我得到这个: 在此处输入图片说明

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

Sco*_*ton 6

让我们改用以下代码:

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)))])
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 有效!有效!有效!我的上帝让我挠了很长时间。 (2认同)