matplotlib 如何在Seaborn条形图中使用行作为图例

ukxgm1gy  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(126)

我有一个这样的dataframe:

index = ['Col-45', 'Col-68', 'Col-17', 'Col-69', 'Col-43', 'Col-49', 'Col-91',
       'Col-13', 'Col-14', 'Col-18', 'Col-38', 'Col-37', 'Col-40', 'Col-44',
       'Col-32', 'Col-82', 'Col-75', 'Col-19', 'Col-5', 'Col-6', 'Col-16',
       'Col-4', 'Col-7', 'Col-41', 'Col-10', 'Col-31', 'Col-12', 'Col-11',
       'Col-42', 'Col-30', 'Col-76', 'Col-46', 'Col-83', 'Col-73', 'Col-63',
       'Col-9', 'Col-28', 'Col-51', 'Col-74', 'Col-65', 'Col-50', 'Col-64',
       'Col-86', 'Col-79', 'Col-80', 'Col-81', 'Col-55', 'Col-1', 'Col-57',
       'Col-2', 'Col-61', 'Col-53', 'Col-88', 'Col-47', 'Col-3', 'Col-58',
       'Col-29', 'Col-59', 'Col-8', 'Col-276', 'Col-56', 'Col-62', 'Col-52',
       'Col-54']
Brand = ['LG','LG','LG','LG','LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'Vivo',
         'Vivo', 'Vivo', 'Vivo', 'Vivo', 'Vivo', 'Vivo', 'Sony', 'Sony', 'Sony', 'Sony', 'Sony', 'Sony', 'Sony', 
         'Pixel', 'Pixel', 'Pixel', 'Pixel', 'Huawei', 'Huawei', 'Huawei', 'Apple', 'Apple', 'Apple', 'Xiaomi',
         'Xiaomi', 'Xiaomi', 'Lenovo', 'Lenovo', 'Lenovo', 'Panasonic', 'Panasonic', 'Panasonic', 'Beetle', 
         'Beetle', 'Samsung', 'Samsung', 'Nothing', 'Nothing', 'Nikon', 'Nikon', 'Canon', 'Canon', 'Coby', 
         'Coby', 'Onida', 'Amara', 'Roxy']

Score = [4.75, 0.91, 0.79, 0.65, 0.62, 0.57, 0.38, 0.33, 0.27, 0.25, 0.25,
       0.22, 0.16, 0.11, 0.02, 0.01, 3.89, 3.08, 2.1 , 1.75, 0.42, 0.27,
       0.18, 4.44, 1.18, 0.8 , 0.74, 0.52, 0.25, 0.08, 1.13, 0.75, 0.54,
       0.04, 1.03, 0.11, 0.  , 5.53, 5.24, 4.98, 0.98, 0.78, 0.06, 0.76,
       0.28, 0.04, 1.1 , 0.38, 0.25, 0.98, 0.01, 1.17, 0.61, 0.29, 0.19,
       0.12, 0.01, 0.13, 0.  , 4.37, 3.59, 0.53, 0.39, 1.3 ]

Choice = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
       0, 0]

Result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
       0]

colors = ['blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue',
          'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 
          'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'pink', 'pink', 'pink', 
          'pink', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 
          'blue', 'blue', 'blue', 'blue', 'blue', 'green', 'red', 'red', 'blue', 'blue', 'blue', 'blue']

col_d = {'blue':'Not Detected by Choice & Result', 'green':'Detected by Choice', 'pink':'Detected by Result','red':'Detected by Choice & Result'}

df = pd.DataFrame({'index':index,'Brand':Brand,'Score':Score,'Choice':Choice,'Result':Result}).set_index('index').T
df.loc['Legend'] = [col_d[i] for i in colors]
display(df)

从这个框架中,我使用seaborn库为索引行Score创建了一个条形图。
这也很有效。

fig = plt.figure()
sns.set(rc={'figure.figsize': (15,4)})
g1 = sns.barplot(data=df.loc[['Score']],palette=colors,hue=df.loc['Legend'])
g1.patch.set_edgecolor('black')  
g1.patch.set_linewidth(0.5)  
g1.set_facecolor('white')
g1.set_ylabel(f'Brand',weight='bold',fontsize=15)
g1.set_xlabel(None)
g1.set_xticklabels(g1.get_xticklabels(),rotation=90,fontsize=10)
plt.show()

然后我想使用索引行Legend作为条形图中的色调。这给了我ValueError:Cannot use hue without x and y
有人能帮我吗?

t3irkdon

t3irkdon1#

只需使用字典(反转col_d)来表示palette,并指定xy

palette = {'Not Detected by Choice & Result': 'blue',
          'Detected by Result': 'pink',
          'Detected by Choice': 'green',
          'Detected by Choice & Result': 'red'}
g1 = sns.barplot(y=df.loc['Score'], x=df.columns, 
                 hue=df.loc['Legend'], 
                 dodge=False, palette=palette)

输出:

尽管建议使用长格式数据:

g1 = sns.barplot(data=df.loc[['Score', 'Legend']].T.reset_index(), 
                 x='index', y='Score', hue='Legend', palette=palette, dodge=False)

相关问题