简体   繁体   English

在 matplotlib 中的图形中添加文本

[英]Add text to a figure in matplotlib

I want to add some text to a 3D wireframe plot.我想在 3D 线框 plot 中添加一些文本。 I am starting with the code from this example in the matplotlib gallery.我从 matplotlib 库中此示例的代码开始。 From the Axes documentation I found a text() .Axes文档中,我找到了一个text() If I'm reading this correctly, there are 4 required positional arguments (including self ).如果我没看错,则需要 4 个位置 arguments (包括self )。 I modified the example as follows:我将示例修改如下:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)

# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
ax.text(0, 0, "I'm here")
plt.show()

When I run this code, I get当我运行这段代码时,我得到

TypeError: text() missing 1 required positional argument: 's'类型错误:文本()缺少 1 个必需的位置参数:'s'

How do I fix this?我该如何解决? What am I doing wrong here?我在这里做错了什么?

In this case you're not dealing with an Axes object, but rather an Axes3D object.在这种情况下,您处理的不是Axes object,而是Axes3D object。 So you need to provide three coordinate numbers to its text() method instead of just 2.因此,您需要为其text()方法提供三个坐标数,而不仅仅是 2。

Alternatively you could also use the text2D() method, which does require only two coordinate number input arguments.或者,您也可以使用text2D()方法,它只需要两个坐标数输入 arguments。

help(ax.text) gives the correct documentation: help(ax.text)提供了正确的文档:

Help on method text in module mpl_toolkits.mplot3d.axes3d:

text(x, y, z, s, zdir=None, **kwargs) method of matplotlib.axes._subplots.Axes3DSubplot instance
...

So you need 3 positional coordinates, and no self .所以你需要 3 个位置坐标,没有self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM