Skip to content

[Bug]: Some matplotlib functions do not work anymore with pandas and numpy 1.24.0 #24773

Closed
@tomMoral

Description

@tomMoral

Bug summary

The new release of numpy introduces a change in np.ma.masked_invalid which makes it not compatible with pandas Series anymore. This breaks some functionality of matplotlib like plt.fill_betweenx.

Reported this in numpy/numpy#22829. Not sure where this should be fixed.

Code for reproduction

import matplotlib.pyplot as plt
import pandas as pd

a = pd.Series([1, 2, 3])
plt.fill_betweenx(a, a, a)

Actual outcome


TypeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 plt.fill_betweenx(a, a, a)

File ~/.local/miniconda/envs/test_benchopt/lib/python3.11/site-packages/matplotlib/pyplot.py:2536, in fill_betweenx(y, x1, x2, where, step, interpolate, data, **kwargs)
2532 @_copy_docstring_and_deprecators(Axes.fill_betweenx)
2533 def fill_betweenx(
2534 y, x1, x2=0, where=None, step=None, interpolate=False, *,
2535 data=None, **kwargs):
-> 2536 return gca().fill_betweenx(
2537 y, x1, x2=x2, where=where, step=step, interpolate=interpolate,
2538 **({"data": data} if data is not None else {}), **kwargs)

File ~/.local/miniconda/envs/test_benchopt/lib/python3.11/site-packages/matplotlib/init.py:1423, in _preprocess_data..inner(ax, data, *args, **kwargs)
1420 @functools.wraps(func)
1421 def inner(ax, *args, data=None, **kwargs):
1422 if data is None:
-> 1423 return func(ax, *map(sanitize_sequence, args), **kwargs)
1425 bound = new_sig.bind(ax, *args, **kwargs)
1426 auto_label = (bound.arguments.get(label_namer)
1427 or bound.kwargs.get(label_namer))

File ~/.local/miniconda/envs/test_benchopt/lib/python3.11/site-packages/matplotlib/axes/_axes.py:5381, in Axes.fill_betweenx(self, y, x1, x2, where, step, interpolate, **kwargs)
5379 def fill_betweenx(self, y, x1, x2=0, where=None,
5380 step=None, interpolate=False, **kwargs):
-> 5381 return self._fill_between_x_or_y(
5382 "y", y, x1, x2,
5383 where=where, interpolate=interpolate, step=step, **kwargs)

File ~/.local/miniconda/envs/test_benchopt/lib/python3.11/site-packages/matplotlib/axes/_axes.py:5272, in Axes._fill_between_x_or_y(self, ind_dir, ind, dep1, dep2, where, interpolate, step, **kwargs)
5268 kwargs["facecolor"] =
5269 self._get_patches_for_fill.get_next_color()
5271 # Handle united data, such as dates
-> 5272 ind, dep1, dep2 = map(
5273 ma.masked_invalid, self._process_unit_info(
5274 [(ind_dir, ind), (dep_dir, dep1), (dep_dir, dep2)], kwargs))
5276 for name, array in [
5277 (ind_dir, ind), (f"{dep_dir}1", dep1), (f"{dep_dir}2", dep2)]:
5278 if array.ndim > 1:

File /.local/miniconda/envs/test_benchopt/lib/python3.11/site-packages/numpy/ma/core.py:2360, in masked_invalid(a, copy)
2332 def masked_invalid(a, copy=True):
2333 """
2334 Mask an array where invalid values occur (NaNs or infs).
2335
(...)
2357
2358 """
-> 2360 return masked_where(
(np.isfinite(getdata(a))), a, copy=copy)

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

No error and a figure :)

Additional information

This issue was introduced by numpy/numpy#22046, shipped starting version 1.24.0 of numpy.

Operating system

linux

Matplotlib Version

3.6.2

Matplotlib Backend

TkAgg

Python version

3.11.0

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions