如何在python matplotlib中切换条形图的颜色?

如何在python matplotlib中切换条形图的颜色?,python,pandas,matplotlib,Python,Pandas,Matplotlib,我正在尝试切换条形图的颜色,以便它们始终保持一致。在下面的图表中,我想让JP_销售额在两个图表中都是橙色的,NA_销售额在两个图表中都是蓝色的 第一个图表的代码为: Genre_sales.plot(kind= 'bar', rot=75, figsize = (15,10)) plt.suptitle('Sales by Region and Genre') plt.ylabel('Total Units (Millions)') PercentSales.plot(kind = 'ba

我正在尝试切换条形图的颜色,以便它们始终保持一致。在下面的图表中,我想让JP_销售额在两个图表中都是橙色的,NA_销售额在两个图表中都是蓝色的

第一个图表的代码为:

Genre_sales.plot(kind= 'bar', rot=75, figsize = (15,10))
plt.suptitle('Sales by Region and Genre')
plt.ylabel('Total Units (Millions)')
PercentSales.plot(kind = 'bar', rot = 80, figsize=(15,10))
plt.suptitle('Percentage by Genre Sales by Region')
plt.xlabel('Genre')
plt.ylabel('Percent')
第二个图表的代码是:

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

您可以使用
“set_color()”
函数

因此,在您的情况下,如果要将其设置为红色,您将执行以下操作:

Genre_sales.set_color('r')
plt.show()
同样地:

PercentSales.set_color('b')
plt.show()
plot()有一个color参数,它接受一个列表,在该列表中,您可以按条形图的显示顺序指定颜色。因此,您只需对每个绘图进行以下更改:

#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))
您需要[bar plot,按列指定bar color]中的第二个答案,因此使用字典将名称映射到它们的颜色。