Skip to content

[Bug]: np.nan in PolyCollection linewidths causes huge performance penalty #29439

@raphaelquast

Description

@raphaelquast

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

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy