Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change colours of Pandas bar chart

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

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

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.

like image 930
user4896331 Avatar asked Dec 24 '22 14:12

user4896331


1 Answers

Let's use this code instead:

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

enter image description here

like image 79
Scott Boston Avatar answered Dec 28 '22 23:12

Scott Boston