Skip to content

Matplotlib >= v3.3.3 breaks with pandas.plotting.register_matplotlib_converters(), ax.pcolormesh(), and datetime objects #19871

Closed

Description

@w2naf

Bug report

Bug summary

Matplotlib >= v3.3.3 breaks when using pandas.plotting.register_matplotlib_converters(), ax.pcolormesh(), and and array of datetime.datetime() objects as coordinates.

Code was tested using clean conda environments only differing by Matplotlib version. Versions tested:

  • mpl v3.3.2: Code works
  • mpl v3.3.3: Code breaks
  • mpl v3.4.1: Code breaks

Code for reproduction
The code below will work if the following lines are commented/removed:

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
#!/usr/bin/env python3
from matplotlib import pyplot as plt
import numpy as np
import datetime

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

fig = plt.figure()
ax  = fig.add_subplot(111)

times   = [datetime.datetime(2021,1,1)]
while len(times) < 721:
    times.append(times[-1]+datetime.timedelta(seconds=120))

y_vals  = np.arange(101)

time_axis, y_axis   = np.meshgrid(times,y_vals)
shape               = (len(y_vals)-1,len(times)-1)
z_data              = np.arange(shape[0]*shape[1])
z_data.shape        = shape

im = ax.pcolormesh(time_axis, y_axis, z_data)
fig.savefig('bug_report.png')

Actual outcome

This traceback comes from my Matplotlib 3.4.1 conda environment:

Traceback (most recent call last):
  File "./bug_report.py", line 23, in <module>
    im = ax.pcolormesh(time_axis, y_axis, z_data)
  File "/home/w2naf/anaconda3/envs/mpl3.4.1/lib/python3.7/site-packages/matplotlib/__init__.py", line 1352, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/home/w2naf/anaconda3/envs/mpl3.4.1/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 6163, in pcolormesh
    shading=shading, kwargs=kwargs)
  File "/home/w2naf/anaconda3/envs/mpl3.4.1/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5655, in _pcolorargs
    Nx = X.shape[-1]
AttributeError: 'list' object has no attribute 'shape'

This traceback comes from my Matplotlib 3.3.3 conda environment:

Traceback (most recent call last):
  File "./bug_report.py", line 23, in <module>
    im = ax.pcolormesh(time_axis, y_axis, z_data)
  File "/home/w2naf/anaconda3/envs/mpl3.3.3/lib/python3.7/site-packages/matplotlib/__init__.py", line 1447, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/home/w2naf/anaconda3/envs/mpl3.3.3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 6093, in pcolormesh
    shading=shading, kwargs=kwargs)
  File "/home/w2naf/anaconda3/envs/mpl3.3.3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5590, in _pcolorargs
    Nx = X.shape[-1]
AttributeError: 'list' object has no attribute 'shape'

Expected outcome
Output from my Matplotlib 3.3.2 conda environment:
bug_report

Matplotlib version

  • Operating system: Ubuntu 18.04.5 LTS
  • Matplotlib version: Code breaks in v3.3.3 and v3.4.1; Code works in v3.3.2
  • Matplotlib backend: Qt5Agg
  • Python version: 3.7.7
  • Other libraries: Pandas v1.2.3

Matplotlib and python were installed with Anaconda for Linux 64 Bit. (conda 4.9.2)

Activity

  1. jklymak commented on Apr 5, 2021

    @jklymak
    Member

    I think this would be more appropriately reported to pandas? We don't maintain matplotlib_converters, and we don't claim they will work. I'm going to close, but feel free to ask for a re-open if there is an identifiable bug on our side.

  2. tacaswell commented on Apr 5, 2021

    @tacaswell
    Member

    I agree the fix for this is probably on the pandas side, but given that this breaks on a patch release we should track down why we got less defensive about the return values from the converters and if that was intended or not.

  3. added this to the v3.4.2 milestone on Apr 5, 2021
  4. jklymak commented on Apr 5, 2021

    @jklymak
    Member
  5. jklymak commented on Apr 5, 2021

    @jklymak
    Member

    The pandas unit converter returns a list from convert instead of a numpy array. If we should be more permissive, I'm not sure where or what the architecture for convert is supposed to be, if it doesn't return a numpy array. But I'm not sure if we specify what convert should do anywhere.

  6. w2naf commented on Apr 6, 2021

    @w2naf
    Author

    Do you still want me to file a pandas bug report, or wait until we have things more resolved here?

  7. added a commit that references this issue on Apr 6, 2021
    7d69aca
  8. tacaswell commented on May 5, 2021

    @tacaswell
    Member

    @w2naf Yes, please report a bug with pandas.

  9. added 2 commits that reference this issue on May 6, 2021
    2fb4d3b
    b30cd83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions