Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Bug]: Invalid text coordinates brick figure #20911

Copy link
Copy link
Open
@anntzer

Description

@anntzer
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.