【问题标题】:Seaborn title error - AttributeError: 'FacetGrid' object has no attribute 'set_titleSeaborn 标题错误 - AttributeError:“FacetGrid”对象没有属性“set_title”
【发布时间】:2020-10-08 08:23:18
【问题描述】:

我首先使用以下代码创建了一个线图:

plot = sns.lineplot(data=tips,
             x="sex",
             y="tip",
             ci=50,
             hue="day",
             palette="Accent")
plot.set_title("Value of Tips Given to Waiters, by Days of the Week and Sex", fontsize=24, pad=30, fontdict={"weight": "bold"})
plot.legend("")

我意识到它实际上是一个我需要的 catplot 图表,因此我将代码修改为以下内容:

plot = sns.catplot (data=tips,
             x="day",
             y="tip",
             kind='bar',
             ci=50,
             hue="sex",
             palette="Accent")
plot.set_title("Value of Tips Given to Waiters, by Days of the Week and Sex", fontsize=24, pad=30, fontdict={"weight": "bold"})
plot.legend("")

但是我收到以下标题错误消息:'AttributeError: 'FacetGrid' object has no attribute 'set_title''。

为什么我的标题不适用于 catplot 图表?

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/questions/43920341/…>

标签: python seaborn


【解决方案1】:

当你调用catplot时,它会返回一个FacetGrid对象,所以要改变标题和删除图例,你必须在函数内部使用legend=选项,并且还要使用plot.fig.suptitle()

import seaborn as sns
tips = sns.load_dataset("tips")
plot = sns.catplot (data=tips,
             x="day",
             y="tip",
             kind='bar',
             ci=50,
             hue="sex",
             palette="Accent", legend=False)

plot.fig.suptitle("Value of Tips Given to Waiters, by Days of the Week and Sex",
                  fontsize=24, fontdict={"weight": "bold"})

【讨论】:

  • 非常感谢,您是如何访问我正在使用的数据集的;)?!
  • 它是 seaborn 包的一部分吗? tips = sns.load_dataset("tips")
  • 从 v0.11.2(2021 年 8 月)开始,不推荐使用 plot.fig,取而代之的是 plot.figure。请参阅corresponding PR
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 2019-08-04
  • 2017-12-17
  • 1970-01-01
相关资源
最近更新 更多