Skip to content

Streamplot color mapping fails on (near-)empty array. #19323

Closed
@juseg

Description

@juseg

Bug report

Bug summary

The streamplot method works on empty (fully masked or np.nan) or nearly empty (too few data points to plot a single streamline) array (for the u and v velocity arguments), unless color mapping is used by providing a 2D array as color= keyword argument.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

# prepare fake data
x = y = np.arange(3)
u = v = np.arange(9).reshape(3, 3)
color = (u**2+v**2)**0.5

# streamplot without color mapping
plt.streamplot(x, y, u, np.ma.masked_greater(v, 4))  # works
plt.streamplot(x, y, u, np.ma.masked_greater(v, 3))  # works
plt.streamplot(x, y, u, np.ma.masked_all_like(v))    # works
plt.streamplot(x, y, u, v*np.nan)                    # works

# streamplot with color mapping
plt.streamplot(x, y, u, np.ma.masked_greater(v, 4), color=color)  # works
plt.streamplot(x, y, u, np.ma.masked_greater(v, 3), color=color)  # fails
plt.streamplot(x, y, u, np.ma.masked_all_like(v), color=color)    # fails
plt.streamplot(x, y, u, v*np.nan, color=color)                    # fails

Actual outcome

Traceback (most recent call last):
  File "/home/julien/emptystreamplot.py", line 19, in <module>
    plt.streamplot(x, y, u, np.ma.masked_greater(v, 3), color=color)  # fails
  File "/usr/lib/python3.9/site-packages/matplotlib/pyplot.py", line 2978, in streamplot
    __ret = gca().streamplot(
  File "/usr/lib/python3.9/site-packages/matplotlib/__init__.py", line 1447, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/usr/lib/python3.9/site-packages/matplotlib/streamplot.py", line 222, in streamplot
    lc.set_array(np.ma.hstack(line_colors))
  File "/usr/lib/python3.9/site-packages/numpy/ma/extras.py", line 296, in __call__
    _d = func(tuple([np.asarray(a) for a in x]), *args, **params)
  File "<__array_function__ internals>", line 5, in hstack
  File "/usr/lib/python3.9/site-packages/numpy/core/shape_base.py", line 346, in hstack
    return _nx.concatenate(arrs, 1)
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: need at least one array to concatenate

Expected outcome

Because other plotting methods work on empty arrays, I expect streamplot to handle the above case (and draw nothing).

Matplotlib version

  • Operating system: Manjaro Linux
  • Matplotlib version: 3.3.3
  • Matplotlib backend: Qt5Agg
  • Python version: 3.9.1
  • Other libraries: Numpy 1.19.4

Matplotlib and other packages installed via the Manjaro package manager.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions