如何操作分布上的图例?[复制]

How can I manipulate the legend on a distribution? [duplicate]

提问人:OranC 提问时间:11/17/2023 最后编辑:Trenton McKinneyOranC 更新时间:11/17/2023 访问量:28

问:

我是 python 的新手,我很难在分布图中操作图例。传说在情节上占据了自己的空间,而不仅仅是在情节之上。我不喜欢这种显示方式,特别是如果我想让几个地块彼此相邻。这是我在特定分布图方面遇到的问题。

第一个输出具有正确的图例,但位于不需要的位置。

sns.displot(data = test_data_2023, x="happiness_score", kind='kde', hue="region", warn_singular=False)

plt.title('Happiness Distribution', fontsize='15')
plt.xlabel('Happiness Score', fontsize=15)
plt.ylabel('Density', fontsize=15)

图例占用太多空间

Legend taking up too much space

这接近我通过使用 sns.displot(legend=False) 并添加 plt.legend() 来达到我想要的效果。但是,我希望图例按其原始顺序排列。我希望图例首先显示最流行的区域,因此我尝试手动添加标签,但图例本身的顺序错误。

sns.displot(data = test_data_2023, x="happiness_score", kind='kde', hue="region", warn_singular=False, legend=False)

    plt.legend(title='Region', title_fontsize=10, loc='upper left', fontsize='8.5', 
                                                                labels=['Europe',
                                                                         'Africa',
                                                                         'Asia & Pacific',
                                                                         'South/Latin America',
                                                                         'Middle east',
                                                                         'Arab States',
                                                                         'North America',
                                                                         'South/Central America'])
    plt.xlabel('Happiness Score', fontsize=15)
    plt.ylabel('Density', fontsize=15)

传说在情节之上,但传说错误

Legend on top of plot but wrong legend

我尝试的下一件事是使用分类对初始数据帧进行排序,以便标签对齐。但无论如何,它似乎对传奇顺序没有影响。

test_data_2023.region = pd.Categorical(test_data_2023.region,categories=regions_sorted_count)

sns.displot(data = test_data_2023.sort_values('region'), x="happiness_score", kind='kde', hue="region", warn_singular=False, legend=False)

plt.legend(title='Region', title_fontsize=10, loc='upper left', fontsize='8.5', 
                                                            labels=['Europe',
                                                                     'Africa',
                                                                     'Asia & Pacific',
                                                                     'South/Latin America',
                                                                     'Middle east',
                                                                     'Arab States',
                                                                     'North America',
                                                                     'South/Central America'])
plt.xlabel('Happiness Score', fontsize=15)
plt.ylabel('Density', fontsize=15)

具有分类排序的输出

基本上,我希望原始图例位于图表的顶部,而不是在它自己的空间中。我怎样才能做到这一点?

两个图例都存在的情节。左边图例的位置是可取的,但右边图例的内容是正确的。

Plot with both legends present. The placement of the left legend is desirable but the contents of the right legend is correct.

蟒蛇 Seaborn Legend displot

评论

2赞 Trenton McKinney 11/17/2023
使用 和 设置数据帧列顺序将生成有序图例,或传递 和 以移动图例。如重复部分所示。pd.Categoricalordered=Truehue_order=[...]sns.move_legend

答: 暂无答案