0

I would like to change the default color scheme of my pandas plot. I tried with different color schemes through cmap pandas parameter, but when I change it, all bars of my barplot get the same color.

The code I tried is the following one:

yearlySalesGenre = df1.groupby('Genre').Global_Sales.sum().sort_values()
fig = plt.figure()
ax2 = plt.subplot()
yearlySalesGenre.plot(kind='bar',ax=ax2, sort_columns=True, cmap='tab20')
plt.show(fig)

And the data that I plot (yearlySalesGenre) is a pandas Series type:

Genre
Strategy         174.50
Adventure        237.69
Puzzle           243.02
Simulation       390.42
Fighting         447.48
Racing           728.90
Misc             803.18
Platform         828.08
Role-Playing     934.40
Shooter         1052.94
Sports          1249.47
Action          1745.27

Using tab20 cmap I get the following plot:

plot with cmap tab20

I get all bars with the first color of all the tab20 scheme. What I am doing wrong?

Note that if I use the default color scheme of pandas plot, it properly displays all bars with different colors, but the thing is that I want to use a particular color scheme.


As posted, it's a duplicated answer. Just in case, the answer is that pandas makes color schemes based on different columns, not in rows. So to use different colors you can transpose the data + some other stuff (duplicated link), or directly use the matplotlib.pyplot plotting that allows more flexibility (in my case):

plt.bar(range(len(df)), df, color=plt.cm.tab20(np.arange(len(df))))
Aurelie Navir
  • 938
  • 2
  • 7
  • 17

1 Answers1

0

Maybe this is what you want:

df2.T.plot( kind='bar', sort_columns=True, cmap='tab20')

I think the problem you have is that you only have one series. Pandas plot bar will plot separate series (columns) each with its own color, and separate each each bar based on the index. By using .T, the series in your data become multiple columns but within only one index. I am sure you can play with the legendenter image description here to get a better display.

Jorge
  • 2,181
  • 1
  • 19
  • 30