将seaborn情节传奇移到另一个位置?

时间:2014-11-19 14:15:53

标签: python seaborn

我正在使用factorplot(kind="bar")和seaborn。

情节很好,除了传说错位:右边太多,文字从阴影的阴影区域出来。

如何让seaborn将传奇放在其他地方,例如左上角而不是右中角?

6 个答案:

答案 0 :(得分:31)

建立在@ 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")

答案 1 :(得分:12)

修改示例here

您可以使用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")

enter image description here

答案 2 :(得分:6)

在此处签出文档:https://matplotlib.org/users/legend_guide.html#legend-location

添加此命令只是使图例脱离情节:

plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

答案 3 :(得分:3)

这就是我能够将图例移动到绘图内的特定位置并更改绘图的外观和大小的方法:

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)  

Legend location changed

这对我有用,可以改变绘图的大小和方面,也可以在绘图区域外移动图例。

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)  

violin plot with changed size, aspect and legend located outside

我从mwaskom的回答here和Fernando Hernandez的回答here中得出这个结论。

答案 4 :(得分:1)

您似乎可以直接致电:

g = sns.factorplot("class", "survived", "sex",
                data=titanic, kind="bar",
                size=6, palette="muted",
               legend_out=False)

g._legend.set_bbox_to_anchor((.7, 1.1))

答案 5 :(得分:0)

如果您想自定义图例,只需使用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.)