首页 > 解决方案 > 在 Relplot 上为标记添加标签

问题描述

我对使用 seaborn 为我的标记添加标签的最佳方法有点迷失了relplot。我在 matplotlib 文档中看到有一种axes.text()方法看起来是正确的方法,但似乎这种方法并不存在。在这个意义上,seaborn 的行为是否与 matplotlib 不同?正确的方法是什么?

错误:

AttributeError: 'numpy.ndarray' object has no attribute 'text'

代码:

line_minutes_asleep = sns.relplot(
    x = "sleep_date",
    y = "minutes_asleep",
    kind = "line",
    data = df,
    height=10, # make the plot 5 units high
    aspect=3
)

x = df.sleep_date
y = df.minutes_asleep
names = df.minutes_asleep

print(line_minutes_asleep.axes.text())

标签: matplotlibseaborn

解决方案


relplot返回一个 FacetGrid,它是一个包含多个子图的图形。.axesFacetGrid的属性是Axes对象的 2D ndarray。因此,您需要使用FacetGrid.axes[i,j]来获取对子图的引用。

如果你想在第一个子图 (axes[0,0]) 中的 x,y=(20,5) 位置写一些东西,你需要这样做:

import seaborn as sns
sns.set(style="ticks")
tips = sns.load_dataset("tips")
g = sns.relplot(x="total_bill", y="tip", hue="day", data=tips)

g.axes[0,0].text(20,5,"this is a text")

推荐阅读