如何手动为分类变量的类型分配颜色?

问题描述 投票:0回答:1

我正在以下代码中创建2个图。我在JobDomain列中有类别值,为[]

  • Cat1
  • Cat2
  • Cat3
  • 下面的代码为上述类别生成2个具有不同颜色的绘图。对于这3个类别,我需要使两个图具有相同的颜色。

colors = ["#F28E2B", "#4E79A7","#79706E"]

edu = (df.groupby(['JobDomain'])['sal']
                         .value_counts(normalize=True)
                         .rename('Percentage')
                         .mul(100)
                         .reset_index()
                         .sort_values('sal'))

coding = (df.groupby(['JobDomain'])['sal2']
                         .value_counts(normalize=True)
                         .rename('Percentage')
                         .mul(100)
                         .reset_index()
                         .sort_values('sal2'))

fig, axs = plt.subplots(ncols=2,figsize=(20, 6),sharey=True)

plt.subplots_adjust(wspace=0.4)

p=sns.barplot(x="sal",y="Percentage",hue="JobDomain",data=edu,
              ax=axs[0],palette=sns.color_palette(colors))
q=sns.barplot(x="sal2",y="Percentage",hue="JobDomain",data=coding,
              ax=axs[1],palette=sns.color_palette(colors))

我正在以下代码中创建2个图。我在JobDomain列中有类别值,如Cat1 Cat2 Cat3。下面的代码正在生成2个图表,每个类别都有不同的颜色。我需要...

python matplotlib seaborn
1个回答
0
投票

通过创建将每个类别映射到颜色的字典(并将其传递到palette而不调用sns.color_palette)。一个例子:

© www.soinside.com 2019 - 2024. All rights reserved.