-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Open
Labels
Description
Bug summary
I noticed that the presence of np.nan
in an array of linewidths
of a PolyCollection
works but causes a major performance penalty (larger collections take forever to render).
I know this is somewhat a special case but since I ran into it I thought I'll report it.
In my case I used a pandas.Series
with some missing values as linewidth-array and it took quite a while to figure out that this was the cause of the performance issue.
Code for reproduction
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
import numpy as np
nx, ny = 10, 10
d = 0.5 / max(nx, ny)
x, y = (i.ravel() for i in np.meshgrid(np.linspace(0, 1, nx), np.linspace(0, 1, ny)))
verts = np.stack([np.stack([x, x + d, x + d, x], axis=-1),
np.stack([y, y, y + d, y + d], axis=-1),], axis=2)
fc = np.array(["r" if i%2 == 0 else "g" for i in range(len(verts))])
# the np.nan values here are the cause of the performance issue
# if a float is used, the plot appears as fast as expected
lw = np.array([np.nan if i%2 == 0 else 1 for i in range(len(verts))])
f, ax = plt.subplots()
c = PolyCollection(verts, fc=fc, lw=lw, ec="k")
ax.add_collection(c)
Actual outcome
plot is extremely slow
Expected outcome
plot is as fast as usual
Additional information
No response
Operating system
No response
Matplotlib Version
3.9.2
Matplotlib Backend
qt
Python version
No response
Jupyter version
No response
Installation
conda