Skip to content

[Bug]: PolyCollection set_offsets does not accept [[], []] like Line2D #22401

Open
@ianhi

Description

@ianhi

Bug summary

The return value of plot a Line2D lets you set_data as [[], []] this is nice because you can do both of

line = plt.plot([],[])[0]
line.set_data([[],[]])

allowing you to make a line disappear by setting it's data to empty. This comes up surprisingly often for me when using mpl-interactions to explore datasets

However, plt.scatter does not have the same symmetry of behavior. You can do

scatter = plt.scatter([[], [])

but you cannot then do

scatter.set_offsets([[],[]])

Code for reproduction

# works
line = plt.plot([],[])[0]

# works
line.set_data([[],[]])

# works
scatter = plt.scatter([],[])

# fails
scatter.set_offsets([[],[]])

Actual outcome

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [76], in <module>
      1 scatter = plt.scatter([],[])
----> 2 scatter.set_offsets([[],[]])

File ~/mambaforge/envs/mpl-inter/lib/python3.9/site-packages/matplotlib/collections.py:566, in Collection.set_offsets(self, offsets)
    563 if offsets.shape == (2,):  # Broadcast (2,) -> (1, 2) but nothing else.
    564     offsets = offsets[None, :]
    565 self._offsets = np.column_stack(
--> 566     (np.asarray(self.convert_xunits(offsets[:, 0]), 'float'),
    567      np.asarray(self.convert_yunits(offsets[:, 1]), 'float')))
    568 self.stale = True

IndexError: index 0 is out of bounds for axis 1 with size 0

Expected outcome

No error and the scatter should not be drawn.

Additional information

No response

Operating system

No response

Matplotlib Version

3.5.1

Matplotlib Backend

any

Python version

3.9.9

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions