Skip to content
Advertisement

How do you switch the colors of a bar chart in python matplotlib?

I’m trying to switch the colors of my bar charts so that they’re consistent throughout. In the plots below, I want to make it so JP_Sales is orange in both charts and NA_Sales is blue in both charts.

current plot

second plot

The code for the first chart is:

Genre_sales.plot(kind= 'bar', rot=75, figsize = (15,10))
plt.suptitle('Sales by Region and Genre')
plt.ylabel('Total Units (Millions)')

The code for the second chart is:

PercentSales.plot(kind = 'bar', rot = 80, figsize=(15,10))
plt.suptitle('Percentage by Genre Sales by Region')
plt.xlabel('Genre')
plt.ylabel('Percent')

Advertisement

Answer

plot() has a color argument which takes a list in which you can specify the colors in the order in which the bars appear. So all you have to do is the following change for each plot:

#for Genre_sales if JP_sales is the first column and NA_sales is the second column:
Genre_sales.plot(kind = 'bar', rot = 75, color = ['orange', 'blue', 'green'], figsize = (15,10))

#for PercentSales if NA_sales is the first column and JP_sales is the second column:
PercentSales.plot(kind = 'bar', rot = 75, color = ['blue', 'orange', 'green', 'red'], figsize = (15,10))
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement