Skip to content

pcolormesh throws UFuncTypeError if np.datetime64() and shading='nearest #19191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Yoshi2112 opened this issue Dec 29, 2020 · 1 comment
Closed

Comments

@Yoshi2112
Copy link

Yoshi2112 commented Dec 29, 2020

Bug report

Bug summary

For people who use numpy.datetime64() arrays with pcolormesh, a DeprecationWarning has been appearing prompting users to specify a shading kwarg 'nearest', 'auto' or 'gouraud' since shading='flat' is being deprecated. However, passing 'nearest' results in a UFuncTypeError when using 'nearest' due to a casting issue in _axes.py from _interp_grid()

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

times = np.arange(np.datetime64('2020-12-01'), np.datetime64('2020-12-31'))
yaxis = np.arange(0, 20)
caxis = np.random.normal(0.0, 1.0, (times.shape[0], yaxis.shape[0]))

plt.pcolormesh(times, yaxis, caxis.T, shading='nearest')
plt.show()

Actual outcome

Traceback (most recent call last):

  File "..\test.py", line 15, in <module>
    plt.pcolormesh(times, yaxis, caxis.T, shading='nearest')

  File "C:\Users\iarey\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2801, in pcolormesh
    __ret = gca().pcolormesh(

  File "C:\Users\iarey\anaconda3\lib\site-packages\matplotlib\__init__.py", line 1438, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)

  File "C:\Users\iarey\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 6093, in pcolormesh
    X, Y, C, shading = self._pcolorargs('pcolormesh', *args,

  File "C:\Users\iarey\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5651, in _pcolorargs
    X = _interp_grid(X)

  File "C:\Users\iarey\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5633, in _interp_grid
    if not (np.all(dX >= 0) or np.all(dX <= 0)):

UFuncTypeError: Cannot cast ufunc 'greater_equal' input 0 from dtype('<m8[D]') to dtype('<m8') with casting rule 'same_kind'

Expected outcome
A pcolormesh timeseries plot with random data and a np.datetime64() x-axis.

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.3.2 via standard conda
  • Matplotlib backend: Qt5Agg
  • Python version: 3.8.5
  • Jupyter version (if applicable):
  • Other libraries: numpy-1.19.2
@jklymak
Copy link
Member

jklymak commented Dec 29, 2020

This is a duplicate of #18547, and closed by #18549 released with 3.3.3

In the case of getting the old behaviour you can just drop the last element (like flat does) if you can't upgrade for some reason (caxis[:-1, :-1].T), or ignore the deprecation.

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

No branches or pull requests

2 participants