将 seaborn 情节图例移动到不同的位置 [英] Move seaborn plot legend to a different position

查看:45
本文介绍了将 seaborn 情节图例移动到不同的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 factorplot(kind="bar") 与 seaborn 一起使用.

I'm using factorplot(kind="bar") with seaborn.

情节很好,只是图例放错了位置:太靠右了,文本超出了情节的阴影区域.

The plot is fine except the legend is misplaced: too much to the right, text goes out of the plot's shaded area.

如何让 seaborn 将图例放置在其他位置,例如左上角而不是右中角?

How do I make seaborn place the legend somewhere else, such as in top-left instead of middle-right?

推荐答案

基于@user308827 的回答:您可以在 factorplot 中使用 legend=False 并通过 matplotlib 指定图例:

Building on @user308827's answer: you can use legend=False in factorplot and specify the legend through matplotlib:

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

titanic = sns.load_dataset("titanic")

g = sns.factorplot("class", "survived", "sex",
                   data=titanic, kind="bar",
                   size=6, palette="muted",
                   legend=False)
g.despine(left=True)
plt.legend(loc='upper left')
g.set_ylabels("survival probability")

  • plt 作用于当前轴.要从 FacetGrid 获取轴,请使用 fig.
    • g.fig.get_axes()[0].legend(loc='lower left')
      • plt acts on the current axes. To get axes from a FacetGrid use fig.
        • g.fig.get_axes()[0].legend(loc='lower left')
        • 这篇关于将 seaborn 情节图例移动到不同的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆