Description
Bug summary
text() doesn't validate its x, y coordinates except at draw time, which mean that calling e.g. ax.text() with invalid coordinates in an interactive shell will brick the figure.
Code for reproduction
In [1]: class T: """just a placeholder"""
In [2]: plt.text(T(), T(), "foo")
Out[2]: Text(<__main__.T object at 0x7f4af7d8fb80>, <__main__.T object at 0x7f4af7d8ff70>, 'foo')
Traceback (most recent call last):
<elided>
File "/home/antony/src/extern/matplotlib/lib/matplotlib/text.py", line 830, in get_unitless_position
x = float(self.convert_xunits(self._x))
TypeError: float() argument must be a string or a number, not 'T'
and future plots will also trigger the same exception, unless you realized you can remove the Text with something like ax.texts[-1].remove()
.
Actual outcome
See above.
Expected outcome
Validation when the Text() is constructed.
Note, though that the inputs are not necessarily float(-like): it can be any scalar supported by the units machinery (well, technically, it should match the unit of the axes if that's already set, but at instantiation time the Text doesn't know what Axes it's going to be attached to (it could even end up being a Figure-level text) so let's not get ahead of ourselves).
Operating system
No response
Matplotlib Version
3.4.2.post1804+gb600026fe8
Matplotlib Backend
any
Python version
3.9
Jupyter version
No response
Other libraries
No response
Installation
No response
Conda channel
No response