Skip to content

raise informative error message on misshapen input to set_offsets#22408

Open
ianhi wants to merge 2 commits intomatplotlib:mainfrom
ianhi:offsets
Open

raise informative error message on misshapen input to set_offsets#22408
ianhi wants to merge 2 commits intomatplotlib:mainfrom
ianhi:offsets

Conversation

@ianhi
Copy link
Copy Markdown
Contributor

@ianhi ianhi commented Feb 5, 2022

PR Summary

Fixes: #22401

Importantly allows this:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

scat = ax.scatter([0,.5], [0, .5])
scat.set_offsets([[],[]])
plt.show()

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • [NA] New features are documented, with examples if plot related.
  • [NA] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [NA] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • [NA] Documentation is sphinx and numpydoc compliant (the docs should build without error).

modified = mcollections.Collection(
offsets=np.column_stack([[0, 1], [0, 1]])
)
modified.set_offsets([[], []])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should actually be column_stack and I should get rid of the transpose in the set method. Most usage will be with column_stack i suspect.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check what other set_offsets methods do. We should aim for consistency.

@matplotlib matplotlib deleted a comment from ianhi Feb 10, 2022
@matplotlib matplotlib deleted a comment from ianhi Feb 10, 2022
@tacaswell tacaswell added this to the v3.6.0 milestone Mar 29, 2022
Comment thread lib/matplotlib/collections.py Outdated
(np.asarray(self.convert_xunits(offsets[:, 0]), 'float'),
np.asarray(self.convert_yunits(offsets[:, 1]), 'float')))

if offsets.shape == (2, 0):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if offsets.shape == (2, 0):
if offsets.shape == (0, 2):

Which will require some changes to the tests.

The docs say(N, 2) so this should be the degenerate case of ((N, 2), .., (2, 2), (1, 2), (0, 2)) (the (2,) case is a special case for spelling (1, 2)).

Copy link
Copy Markdown
Contributor Author

@ianhi ianhi Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (0,2) just works fine in the else block? This branch is necessary to handle the special case of offsets=[[],[]]

import numpy as np
np.asanyarray([[],[]]).shape
# (2, 0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah wait I think I understand better. You're saying that [[],[]] is not really a valid input? becuase that's like saying I have two points each with zero data, which doesn't make any sense I guess.

@tacaswell
Copy link
Copy Markdown
Member

Anyone can dismiss my review

@jklymak jklymak marked this pull request as draft March 30, 2022 10:52
@jklymak
Copy link
Copy Markdown
Member

jklymak commented Mar 30, 2022

Fell free to move out of draft...

@tacaswell
Copy link
Copy Markdown
Member

Looking at this again, I think my comment is still valid.

@ianhi ianhi changed the title allow setting collection offsets to empty raise informative error message on misshapen input to set_offsets Dec 12, 2025
@ianhi ianhi marked this pull request as ready for review December 12, 2025 19:03
@ianhi
Copy link
Copy Markdown
Contributor Author

ianhi commented Dec 12, 2025

Looking again myself I now understand and agree with @tacaswell 's comment. and have updated this PR to instead throw the error message that would have set me on the right track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

6 participants