Skip to content

[Bug]: Units behavior of streamplot #27500

Open
@ksunden

Description

@ksunden

Bug summary

As seen in #27489 streamplot does not allow passing unitful data as x/y

Code for reproduction

fig, ax = plt.subplots()

np.random.seed(19680801)
limit_value = 30

date_array = np.array(
        [datetime.datetime(2023, 12, n) for n in range(1, limit_value)]
    , dtype=np.datetime64)
#date_array_converted = mpl.dates.date2num(date_array)
X, Y = np.meshgrid(date_array, date_array)
U = np.random.rand(limit_value-1, limit_value-1)
V = np.random.rand(limit_value-1, limit_value-1)

ax.streamplot(X, Y, U, V)

Actual outcome

Traceback (most recent call last):
  File "/home/kyle/play/gh27487.py", line 22, in <module>
    ax.streamplot(X, Y, U, V)
  File "/home/kyle/src/scipy/matplotlib/lib/matplotlib/__init__.py", line 1472, in inner
    return func(
           ^^^^^
  File "/home/kyle/src/scipy/matplotlib/lib/matplotlib/streamplot.py", line 91, in streamplot
    grid = Grid(x, y)
           ^^^^^^^^^^
  File "/home/kyle/src/scipy/matplotlib/lib/matplotlib/streamplot.py", line 330, in __init__
    if not np.allclose(x_row, x):
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/kyle/.pyenv/versions/edge/lib/python3.11/site-packages/numpy/core/numeric.py", line 2241, in allc
lose
    res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kyle/.pyenv/versions/edge/lib/python3.11/site-packages/numpy/core/numeric.py", line 2345, in iscl
ose
    dt = multiarray.result_type(y, 1.)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
numpy.exceptions.DTypePromotionError: The DType <class 'numpy._FloatAbstractDType'> could not be promoted by <c
lass 'numpy.dtypes.DateTime64DType'>. 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 'nump
y.dtypes.DateTime64DType'>, <class 'numpy._FloatAbstractDType'>)

Expected outcome

Units can be passed as at least X/Ya (I could see requiring them to be the same units... or even saying "this type of plot shouldn't support units", but would want to be explicit about that.

I do wonder a bit about U/V and whether they should be specified in delta units or not? quiver can treat them as independent values or as delta values, I believe, but don't see such a distinction on stream lines.

Additional information

May be as easy as calling _process_unit_info on the inputs, I think.

Operating system

No response

Matplotlib Version

3.9.0.dev821+gfa6be74127 (~main at time of reporting)

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

git checkout

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