提问人:OranC 提问时间:11/17/2023 最后编辑:Trenton McKinneyOranC 更新时间:11/17/2023 访问量:28
如何操作分布上的图例?[复制]
How can I manipulate the legend on a distribution? [duplicate]
问:
这个问题在这里已经有答案了:
Pandas DataFrame 按分类列排序,但按特定类排序排序(3 个答案)
如何在 Seaborn 图中设置色调顺序 (2 个答案)
将海生剧情图例移动到不同位置 (8 个答案)
7天前关闭。
我是 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)
图例占用太多空间
这接近我通过使用 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)
传说在情节之上,但传说错误
我尝试的下一件事是使用分类对初始数据帧进行排序,以便标签对齐。但无论如何,它似乎对传奇顺序没有影响。
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)
基本上,我希望原始图例位于图表的顶部,而不是在它自己的空间中。我怎样才能做到这一点?
两个图例都存在的情节。左边图例的位置是可取的,但右边图例的内容是正确的。
答: 暂无答案
评论
pd.Categorical
ordered=True
hue_order=[...]
sns.move_legend