简体   繁体   中英

Add text to a figure in matplotlib

I want to add some text to a 3D wireframe plot. I am starting with the code from this example in the matplotlib gallery. From the Axes documentation I found a text() . If I'm reading this correctly, there are 4 required positional arguments (including 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'

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. So you need to provide three coordinate numbers to its text() method instead of just 2.

Alternatively you could also use the text2D() method, which does require only two coordinate number input arguments.

help(ax.text) gives the correct documentation:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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