python - 将 seaborn 情节图例移动到不同的位置

标签 python matplotlib seaborn legend

我将 factorplot(kind="bar") 与 seaborn 一起使用。

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

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

最佳答案

基于@user308827 的回答:您可以在 factorplot 中使用 legend=False 并通过 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')

关于python - 将 seaborn 情节图例移动到不同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019079/

相关文章:

python - 如何在seaborn线图中的特定点添加标记?

python - Asyncio 两个循环用于不同的 I/O 任务?

python - 我可以在 python 类中有两个 init 函数吗?

python - Matplotlib 图例中的字幕

python - 使用和不使用 make_axes_locatable->divider colorbars 在 matplotlib 中匹配图形大小

python - 如何解决 python 中的图例颜色问题?

python - 使用airflow插件导入外部python函数

python - JSON Python 读取/写入 json 文件问题

python - 设置在 matplotlib 中使用 plt.subplots 创建的图形的高度和宽度?

python - 使用seaborn库从最佳拟合正态分布中获取平均值和标准差