如何在seaborn lmplot上添加标题?

18

我正在尝试在Searborn lmplot上添加标题。

ax = plt.axes()
sns.lmplot(x, y, data=df, hue="hue", ax=ax)
ax.set_title("Graph (a)")
plt.show()

但我发现lmplot没有ax参数。如何在我的lmplot上添加标题?


你遇到了什么错误? - Akash
@Akash lmplot() 函数出现了意外的关键字参数 'ax'。 - jayko03
@jaykodeveloper,你想设置一个共同的标题还是每个子图都有一个标题? - MaxU - stand with Ukraine
lmplots不接受ax参数,因为它们返回facetgrids而不是subplots。通常我只需添加plt.title("your title") - Woody Pride
@WoodyPride 它生成了另一个空图。谢谢你的帮助。 - jayko03
嗯...在我的环境中没有。奇怪。 - Woody Pride
4个回答

17

试试这个:

sns.lmplot(x, y, data=df, hue="hue")
ax = plt.gca()
ax.set_title("Graph (a)")

5
这会生成一个标题为空的图表;lmplot仍然没有标题。 - John Wiseman

11
# Create lmplot
lm = sns.lmplot(x, y, data=df, hue="hue", ax=ax)

# Access the figure
fig = lm.fig 

# Add a title to the Figure
fig.suptitle("My figtitle", fontsize=12)

10

seaborn 使用 matplotlib 作为其底层库,因此如果你需要简单的答案,请使用:

plt.title('My Title')

就在之前

plt.show()

6
sns.lmplot(x, y, data=df, hue="hue", ax=ax).fig.suptitle("Graph (a)")

2
完美!正是我所寻找的! - skad00sh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接