Python 将 seaborn 情节图例移动到不同的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27019079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move seaborn plot legend to a different position?
提问by user124114
I'm using factorplot(kind="bar")with seaborn.
我正在factorplot(kind="bar")与 seaborn 一起使用。
The plot is fine except the legend is misplaced: too much to the right, text goes out of the plot's shaded area.
情节很好,除了图例放错了位置:太靠右了,文本超出了情节的阴影区域。
How do I make seaborn place the legend somewhere else, such as in top-left instead of middle-right?
我如何让 seaborn 将图例放在其他地方,例如左上角而不是右中角?
采纳答案by Jules
Building on @user308827's answer: you can use legend=Falsein factorplot and specify the legend through matplotlib:
基于@user308827 的回答:您可以legend=False在 factorplot 中使用并通过 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")
回答by user308827
Modifying the example here:
在此处修改示例:
You can use legend_out = False
您可以使用 legend_out = False
import seaborn as sns
sns.set(style="whitegrid")
titanic = sns.load_dataset("titanic")
g = sns.factorplot("class", "survived", "sex",
data=titanic, kind="bar",
size=6, palette="muted",
legend_out=False)
g.despine(left=True)
g.set_ylabels("survival probability")


回答by denson
This is how I was able to move the legend to a particular place inside the plot and change the aspect and size of the plot:
这就是我如何能够将图例移动到情节内的特定位置并改变情节的方面和大小:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
import seaborn as sns
sns.set(style="ticks")
figure_name = 'rater_violinplot.png'
figure_output_path = output_path + figure_name
viol_plot = sns.factorplot(x="Rater",
y="Confidence",
hue="Event Type",
data=combo_df,
palette="colorblind",
kind='violin',
size = 10,
aspect = 1.5,
legend=False)
viol_plot.ax.legend(loc=2)
viol_plot.fig.savefig(figure_output_path)
This worked for me to change the size and aspect of the plot as well as move the legend outside the plot area.
这对我来说改变了情节的大小和方面以及将图例移到了情节区域之外。
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
import seaborn as sns
sns.set(style="ticks")
figure_name = 'rater_violinplot.png'
figure_output_path = output_path + figure_name
viol_plot = sns.factorplot(x="Rater",
y="Confidence",
hue="Event Type",
data=combo_df,
palette="colorblind",
kind='violin',
size = 10,
aspect = 1.5,
legend_out=True)
viol_plot.fig.savefig(figure_output_path)
I figured this out from mwaskom's answer hereand Fernando Hernandez's answer here.
回答by leonard
Check out the docs here: https://matplotlib.org/users/legend_guide.html#legend-location
在此处查看文档:https: //matplotlib.org/users/legend_guide.html#legend-location
adding this simply worked to bring legend out of the plot:
添加这个只是为了将传说带出情节:
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
回答by Jacobe2169
If you wish to customize your legend, just use the add_legendmethod. It takes the same parameters as matplotlib plt.legend.
如果您想自定义您的图例,只需使用该add_legend方法。它采用与 matplotlib 相同的参数plt.legend。
import seaborn as sns
sns.set(style="whitegrid")
titanic = sns.load_dataset("titanic")
g = sns.factorplot("class", "survived", "sex",
data=titanic, kind="bar",
size=6, palette="muted",
legend_out=False)
g.despine(left=True)
g.set_ylabels("survival probability")
g.add_legend(bbox_to_anchor=(1.05, 0), loc=2, borderaxespad=0.)

