Skip to content

[Bug]: xlim() and ylim() only accept limits of more limited size than coordinates are allowed to be #32724

Backlog

Description

@ejdp62

Bug summary

In pyplot, when supplied with large enough limits, xlim() and ylim() report an error (see example below),
even when these limits are smaller than allowed coordinates of points in curves.
Coordinates and limits need to be compared, so the different treatment of the two is inconsistent.
Please do not solve this inconsistency by restricting coordinates, but by allowing larger numbers for limits.

Code for reproduction

import matplotlib.pyplot as plt
plt.xlim(-10000000000000000000,10000000000000000000)

Actual outcome


TypeError Traceback (most recent call last)
Cell In[194], line 1
----> 1 plt.xlim(-Integer(10000000000000000000),Integer(10000000000000000000))

File /var/tmp/sage-10.9/local/lib/python3.14/site-packages/matplotlib/pyplot.py:2124, in xlim(*args, **kwargs)
2122 if not args and not kwargs:
2123 return ax.get_xlim()
-> 2124 ret = ax.set_xlim(*args, **kwargs)
2125 return ret

File /var/tmp/sage-10.9/local/lib/python3.14/site-packages/matplotlib/axes/_base.py:3828, in _AxesBase.set_xlim(self, left, right, emit, auto, xmin, xmax)
3826 raise TypeError("Cannot pass both 'right' and 'xmax'")
3827 right = xmax
-> 3828 return self.xaxis._set_lim(left, right, emit=emit, auto=auto)

File /var/tmp/sage-10.9/local/lib/python3.14/site-packages/matplotlib/axis.py:1216, in Axis._set_lim(self, v0, v1, emit, auto)
1213 name = self._get_axis_name()
1215 self.axes._process_unit_info([(name, (v0, v1))], convert=False)
-> 1216 v0 = self.axes._validate_converted_limits(v0, self.convert_units)
1217 v1 = self.axes._validate_converted_limits(v1, self.convert_units)
1219 if v0 is None or v1 is None:
1220 # Axes init calls set_xlim(0, 1) before get_xlim() can be called,
1221 # so only grab the limits if we really need them.

File /var/tmp/sage-10.9/local/lib/python3.14/site-packages/matplotlib/axes/_base.py:3748, in _AxesBase._validate_converted_limits(self, limit, convert)
3745 if isinstance(converted_limit, np.ndarray):
3746 converted_limit = converted_limit.squeeze()
3747 if (isinstance(converted_limit, Real)
-> 3748 and not np.isfinite(converted_limit)):
3749 raise ValueError("Axis limits cannot be NaN or Inf")
3750 return converted_limit

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Expected outcome

A nice plot that is limited in x and/or y direction as requested.

Additional information

According to the error output, there is some coercing going on inside pyplot, which apparently only handles limits of limited size.
I do not have a fix available. And maybe similar inconsistencies occur with other pyplot functions; I only tested xlim() and ylim().
I did try to cast the limits explicitly to a type large enough to hold them, but that did not seem to work.
This is the first time that I supplied such large limits, so I do not know if this is a new issue or not.
(To indicate the context for these plots: the target accuracy set for calculations is currently set at 460 bits.)

Operating system

Ubuntu 24.04.5 LTS

Matplotlib Version

3.10.8

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

module://matplotlib_inline.backend_inline

Jupyter version

Command jupyter is not installed; I use "sage -n" , which activates a jupyter session

Installation

Linux package manager

Activity

  1. added
    Community support
    on Dec 5, 2026
  2. timhoffm commented on Dec 5, 2026

    @timhoffm
    Member

    Matplotlib internally uses numpy. We support everything that numpy does. Your (python native) integer -10000000000000000000 is not representable by numpys largest signed integer type np.uint64. Therefore, processing internally fails.

    You can work around this by using a float -10000000000000000000.0.

    We cannot do an automatic float conversion, because it looses precision and whether that's ok must be explicitly decided by the user.

    I'm unclear whether we can provide a better error message.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions