Skip to content

Should xlim support single-entry arrays? #15690

Closed
@cdeil

Description

@cdeil

There is one change in behaviour I noticed when upgrading to Python 3.8, where matplotlib 3.1.1 used to work in Python 3.7, but not in Python 3.8 any more.

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> plt.xlim(np.array([3]), 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/deil/opt/anaconda3/envs/py38/lib/python3.8/site-packages/matplotlib/pyplot.py", line 1434, in xlim
    ret = ax.set_xlim(*args, **kwargs)
  File "/Users/deil/opt/anaconda3/envs/py38/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3270, in set_xlim
    left, right = sorted([left, right], reverse=reverse)
TypeError: only integer scalar arrays can be converted to a scalar index

I think the issue is that reverse = np.array([False]) and sorted refuses to process that, raising the TypeError. I'm not sure if this is a change in behaviour in CPython, or if I'm having slightly different Numpy & matplotlib versions in my Python 3.7 and 3.8 environments.

In our scripts we have code that computes the left and right for set_xlim, and for complex reasons it gives a 1-element ndim=1 Numpy array, not a Numpy scalar.

Now the docstring says that only scalar is supported:
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.set_xlim.html

Is the old behaviour to work OK with single-entry ndim=1 arrays something you want to support via some code change in matplotlib?
Or is this a usage bug that only accidentally worked earlier, and we fix our code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions