Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: pcolorfast does not support units for X/Y #27526

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

Open
QuLogic opened this issue Dec 16, 2023 · 2 comments
Open

[Bug]: pcolorfast does not support units for X/Y #27526

QuLogic opened this issue Dec 16, 2023 · 2 comments

Comments

@QuLogic
Copy link
Member

QuLogic commented Dec 16, 2023

Bug summary

As found in #27450, the X/Y arguments to pcolorfast do not support units.

Code for reproduction

import datetime

import numpy as np
import matplotlib.pyplot as plt


np.random.seed(19680801)

basedate_x = datetime.datetime(2023, 12, 6, 1, 30, 30)
basedate_y = datetime.datetime(2024, 5, 5, 12, 15, 45)
dates_x = [basedate_x + datetime.timedelta(days=1*i, hours=6*i, minutes=20*i)
           for i in range(10)]
dates_y = [basedate_y + datetime.timedelta(days=1*i, hours=8*i, minutes=30*i)
           for i in range(10)]

data = np.random.rand(0, 100)

fig, ax = plt.subplots()
pc = ax.pcolorfast(dates_x, dates_y, data)

Actual outcome

Traceback (most recent call last):
  File "/home/elliott/code/matplotlib/pcolorfast.py", line 19, in <module>
    pc = ax.pcolorfast(dates_x, dates_y, data)
  File "/home/elliott/code/matplotlib/lib/matplotlib/__init__.py", line 1472, in inner
    return func(
  File "/home/elliott/code/matplotlib/lib/matplotlib/axes/_axes.py", line 6527, in pcolorfast
    self.update_datalim(np.array([[xl, yb], [xr, yt]]))
  File "/home/elliott/code/matplotlib/lib/matplotlib/axes/_base.py", line 2503, in update_datalim
    if not np.any(np.isfinite(xys)):
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

A plot similar to pcolormesh, with dates on the x/y axis.

Additional information

No response

Operating system

No response

Matplotlib Version

589d3fb

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

git checkout

@dstansby
Copy link
Member

Using non-datetime units (from pint) works fine and the axes are labelled correctly, so this is perhaps an issue specifically with datetime or list of datetimes?

import matplotlib.pyplot as plt
import numpy as np

import pint
ureg = pint.UnitRegistry()
ureg.setup_matplotlib()

X, Y = np.meshgrid(np.linspace(-3, 3, 16), np.linspace(-3, 3, 16))
C = X**2 + Y**2

X = X * ureg.m
Y = Y * ureg.m

fig, ax = plt.subplots()
ax.pcolormesh(X, Y, C)
plt.show()

@dstansby
Copy link
Member

Specifically the issue seems to be _AxesBase.update_datalim() does not like being given List[Tuple[datetime.datetime, datetime.datetime] becasue np.isfinite doesn't work on that type.

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