Skip to content

Better document and error handle third dimension in pyplot.text() positional argument #19446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nc011 opened this issue Feb 3, 2021 · 6 comments · Fixed by #19455
Closed

Comments

@nc011
Copy link

nc011 commented Feb 3, 2021

Problem

The documentation for pyplot.text() does not specifically mention how to use in 3d projection plots (it only says two positional arguments are accepted).

matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=<deprecated parameter>, **kwargs)

Additionally, the error thrown if a third dimension is not included is incorrect. For example:

import matplotlib.pyplot as plt

fig = plt.figure(constrained_layout=True, figsize = [11, 8])
gs = fig.add_gridspec(2, 2)
ax = fig.add_subplot(gs[0, 1], projection='3d')
ax.text(0.0,20,"test", size=12)

gives the error:
TypeError: text() missing 1 required positional argument: 's'

when, in fact, the error is the missing z position argument.

Suggested Improvement

The documentation could be improved to show that z can (and should) be used in 3d projections. Additionally, the error should be updated to reflect the actual issue (a missing third positional argument).

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.3.2
  • Matplotlib documentation version: 3.1.2
@QuLogic
Copy link
Member

QuLogic commented Feb 3, 2021

There are 4 required positional arguments, and you passed 3. Thus the last one (s) is missing; this is an error message generated by Python, and I don't think we should try to guess which argument was forgotten.

@timhoffm
Copy link
Member

timhoffm commented Feb 3, 2021

In general, pyplot cannot be used to manipulate 3D plots. As you have correctly observed, the function signatures apply to 2D only.

Concerning the "missing" argument: since you created a Axes3D, the signature for text() is

text(self, x, y, z, s, zdir=None, **kwargs)

The message TypeError: text() missing 1 required positional argument: 's' is correct because you have provided 3 parameters, which will be bound to x, y, z. So the value for s is indeed missing. This parameter binding is core python functionality and it's not possible to intercept it or manipulate the error message.

@timhoffm
Copy link
Member

timhoffm commented Feb 4, 2021

IMHO the only thing that could be done, is a note here that pyplot cannot be used for 3D plots. Something along the lnes of:

Note:
pyplot cannot be used for 3D plots, because it's function sigantures are strictly 2D and cannot provide the additional information needed for 3D. Instead, use the explicit API by calling the respective methods on the Axes3D object.

@nc011
Copy link
Author

nc011 commented Feb 4, 2021

Ah OK - I see. That does make sense.

I hadn't noticed text3D(self, x, y, z, s, zdir=None, **kwargs).

@anntzer
Copy link
Contributor

anntzer commented Feb 4, 2021

We could (not clear whether we should) make the decorator for pyplot functions create a wrapper function that first checks whether gca() is a 3D axes and warns/raises accordingly, even before actually forwarding the parameters.

@timhoffm
Copy link
Member

timhoffm commented Feb 4, 2021

Given that mplot3d is an addon package and not core Matplotlib, I feel we should not add additional code to pyplot to just get slightly better error messages. - I acknowlegde however that this is rather an academic argument and that distinction doesn't mean anything to most of our users.

From a practical point of view, AFAIK we have not seen other issues on this, so it does not seem a very pressing problem. If there come additional complaints later, we can always add the check then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants