Matplotlib:annotate()缺少1个必需的位置参数:“self”

2024-10-03 04:36:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我是matplotlib的新手,我试图将文本设置为图形中的某个点,但出现错误:

Traceback (most recent call last): File "main.py", line 239, in main() File "main.py", line 232, in main p.show_graphic_ortg_drtg() File "/home/josecarlos/Workspace/python/process/process.py", line 363, in show_graphic_ortg_drtg Axes.Axes.annotate(xy=(df[0:1]["ortg"], df[0:1]["drtg"]), s="Hola") TypeError: annotate() missing 1 required positional argument: 'self'

我的代码是:

import matplotlib.axes as Axes

Axes.Axes.annotate(xy=(df[0:1]["ortg"], df[0:1]["drtg"]), s="Message")

df是以前生成的熊猫数据帧。在

我做错什么了?我遵循了一些教程和文档,我没有发现错误。在


Tags: inpydfmatplotlibmainshowlineprocess
2条回答

不能直接从类调用非静态方法。首先需要实例化Axis对象。在

有很多方法可以获得Axes实例。简单而紧凑的方法是:

fig, ax = plt.subplots()
# this function returns an instance of the Figure class
# and an instance of the Axes class.
ax.annotate(...)
# call annotate() from the Axes instance

不能直接从类导入它。在

简要说明:

fig, ax = plt.subplots()
ax.annotate(.....)

示例(取自the documentation):

^{pr2}$

参考号:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.annotate.html

相关问题 更多 >