matplotlib - 在 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())
解决方案
relplot
返回一个 FacetGrid,它是一个包含多个子图的图形。.axes
FacetGrid的属性是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")
推荐阅读
- python - 如何在 amazon sagemaker 上部署 xgboost 模型?
- electron - Electron:如何在拔出 pendrive 时关闭便携式应用程序
- haproxy - 是否可以在 HAProxy ACL 语句中比较两个变量?
- python - pip install osmnx 给出错误代码 1
- apache-kafka - 如何在 kafka 中创建定期休息 api 调用的变更日志
- javascript - 填充缓存数据
- python - Tensorflow GAN 仅在批量大小等于 1 时有效
- ubuntu - AMD GPU 上的 TensorFlow
- java - Java 无效标志错误
- c# - .Net 核心身份单元测试 - 将数据添加到模拟用户存储