-2

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?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268

2 Answers2

1

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.

Xukrao
  • 8,003
  • 5
  • 26
  • 52
  • Thanks for the answer. This leads me to believe that what I'm trying to do isn't quite what I want. I found that `fig.text()` will work better for what I need. – Code-Apprentice Oct 14 '19 at 21:23
1

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.

peer
  • 4,171
  • 8
  • 42
  • 73
  • Why doesn't help show `self` as one of the arguments? – Code-Apprentice Oct 14 '19 at 21:22
  • @Code-Apprentice The [`self`](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self-in-python) object is always passed automatically by python. – Xukrao Oct 14 '19 at 21:57
  • @Xukrao Exactly. In code, we explicitly list it as a parameter. This is why I'm asking why it doesn't appear in the help here? Perhaps it just assumes that the reader knows it is passed in. – Code-Apprentice Oct 14 '19 at 22:34