Skip to content

[Bug]: timedelta64 with pcolormesh #26525

@jklymak

Description

@jklymak

Bug summary

timedelta64 doesn't work with pcolormesh, and many other methods.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

time = np.arange(0, 50, dtype='timedelta64[h]')
y = np.arange(10)
Z = np.random.randn(10, 50)

fig, ax = plt.subplots()
ax.pcolormesh(time, y, Z)

Actual outcome

Traceback (most recent call last):
  File "/Users/jklymak/matplotlib/testit.py", line 9, in <module>
    ax.pcolormesh(time, y, Z)
  File "/Users/jklymak/matplotlib/lib/matplotlib/__init__.py", line 1465, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jklymak/matplotlib/lib/matplotlib/axes/_axes.py", line 6270, in pcolormesh
    X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jklymak/matplotlib/lib/matplotlib/axes/_axes.py", line 5854, in _pcolorargs
    X = _interp_grid(X)
        ^^^^^^^^^^^^^^^
  File "/Users/jklymak/matplotlib/lib/matplotlib/axes/_axes.py", line 5833, in _interp_grid
    dX = np.diff(X, axis=1)/2.
         ~~~~~~~~~~~~~~~~~~^~~
  File "/Users/jklymak/mambaforge/envs/mpl-dev/lib/python3.11/site-packages/numpy/ma/core.py", line 4254, in __truediv__
    return true_divide(self, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jklymak/mambaforge/envs/mpl-dev/lib/python3.11/site-packages/numpy/ma/core.py", line 1171, in __call__
    m |= domain(da, db)
         ^^^^^^^^^^^^^^
  File "/Users/jklymak/mambaforge/envs/mpl-dev/lib/python3.11/site-packages/numpy/ma/core.py", line 858, in __call__
    return umath.absolute(a) * self.tolerance >= umath.absolute(b)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: The DType <class 'numpy.dtype[timedelta64]'> could not be promoted by <class 'numpy.dtype[float64]'>. This means that no common DType exists for the given inputs. For example they cannot be stored in a single array unless the dtype is `object`. The full list of DTypes is: (<class 'numpy.dtype[timedelta64]'>, <class 'numpy.dtype[float64]'>)

Expected outcome

The expected outcome is debatable. We have tried to have this go through units like datetime64. However, I really think we should just convert to float. Yeah, that will give giant numbers for timedelta64 of ns, but I think it's better than failing.

Additional information

No response

Operating system

No response

Matplotlib Version

main

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

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