8000 Optimisation in set_verts causes error if `verts` have irregular sizes · Issue #18017 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Optimisation in set_verts causes error if verts have irregular sizes #18017
Closed
@MichaelClerx

Description

@MichaelClerx

Bug report

This optimisation fails if verts is an ndarray containing lists of varying sizes (e.g. regions with different numbers of points)

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.collections import PolyCollection

# Vertices: irregular shapes
regions = (
    ((1, 1), (1, 2), (2, 2)),
    ((3, 3), (3, 4), (4, 4), (6, 4)),
)

# Use numpy to select vertices (in real life not through a static list)
selection = np.array([1, 0, 1])
regions = np.array(regions, dtype=object)
regions = regions[selection]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.set_xlim(0, 10)
ax.set_ylim(0, 10)

c = PolyCollection(regions)
ax.add_collection(c)

plt.show()

Actual outcome

Traceback (most recent call last):
  File "./ff", line 24, in <module>
    c = PolyCollection(regions)
  File "/home/michael/dev/pints/venv/lib/python3.8/site-packages/matplotlib/collections.py", line 1125, in __init__
    self.set_verts(verts, closed)
  File "/home/michael/dev/pints/venv/lib/python3.8/site-packages/matplotlib/collections.py", line 1153, in set_verts
    verts_pad = np.concatenate((verts, verts[:, :1]), axis=1)
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

Expected outcome

Works as normal

Matplotlib version
Python3.8 and matplotlib 3.3.0

Workaround is to just do a .tolist() before passing regions to matplotlib. But the line that checks if its an array should probably check if it's a rectangular array

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0