matplotlib 将海运图图例移动到其他位置

sxpgvts3  于 2023-02-13  发布在  其他
关注(0)|答案(8)|浏览(319)

我在seaborn中使用factorplot(kind="bar")
绘图很好,只是图例放错了位置:太靠右,文本会超出图的阴影区域。
我如何让seaborn把图例放在其他地方,比如左上角而不是右中角?

lg40wkob

lg40wkob1#

基于@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获取轴,请使用图
  • g.fig.get_axes()[0].legend(loc='lower left')
kpbwa7wx

kpbwa7wx2#

import matplotlib.pyplot as plt
import seaborn as sns

# load the data
penguins = sns.load_dataset('penguins', cache=False)

图级图

g = sns.displot(penguins, x="bill_length_mm", hue="species", col="island", col_wrap=2, height=3)
sns.move_legend(g, "upper left", bbox_to_anchor=(.55, .45), title='Species')
plt.show()

轴水平图

ax = sns.histplot(penguins, x="bill_length_mm", hue="species")
sns.move_legend(ax, "lower center", bbox_to_anchor=(.5, 1), ncol=3, title=None, frameon=False)
plt.show()

kuarbcqp

kuarbcqp3#

查看此处的文档:https://matplotlib.org/users/legend_guide.html#legend-location
加上这一点只是为了把传奇从情节中带出来:
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

piztneat

piztneat4#

修改示例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")

ovfsdjhp

ovfsdjhp5#

这就是我如何能够将图例移动到图中的特定位置并更改图的纵横比和大小:

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)

这对我改变情节的大小和方面以及将图例移到情节区域之外起到了作用。

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)

我从mwaskom的答案here和费尔南多·埃尔南德斯的答案here中得出了这个结论。

yxyvkwin

yxyvkwin6#

你好像可以直接打电话:

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))
9fkzdhlc

9fkzdhlc7#

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

smdncfj38#

使用面向对象的API:

fig,ax = plt.subplots(1,1)
sns.someplot(...,ax=ax)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels,loc="upper left")

来源:https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html

相关问题