Skip to content

Passing an incorrectly sized colour list to scatter should raise a relevant error #11373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ImportanceOfBeingErnest opened this issue Jun 4, 2018 · 4 comments
Milestone

Comments

@ImportanceOfBeingErnest
Copy link
Member

ImportanceOfBeingErnest commented Jun 4, 2018

Bug report

Bug summary

When calling a scatter with a list for the c argument, which does not match the length of the data, an AttributeError: 'list' object has no attribute 'shape' is raised instead of informing the user about the non-matching length issue.

#7363 tried hard to make error message clearer but forgot to consider that input may be any sequence, not necessarily a numpy array.
A similar issue was raised in #9209.

Code for reproduction

import matplotlib.pyplot as plt
x = [1,2,3,4,5]
c = [1,2,3,4,5,6] # one color more than values
plt.scatter(x,x, c=c)
plt.show()

Actual outcome

An error

File "...\lib\site-packages\matplotlib\axes\_axes.py", line 4279, in scatter
    .format(c.shape, x.size, y.size))

AttributeError: 'list' object has no attribute 'shape'

This error tells nothing about the true cause of the problem, but is rather an error caused by the error message itself.

Expected outcome

An error similar to the intended one

except ValueError:
# c not acceptable as PathCollection facecolor
raise ValueError("c of shape {} not acceptable as a color "
"sequence for x with size {}, y with size {}"
.format(c.shape, x.size, y.size))

but for a list, which does not have a shape attribute.

Note that I rather raised this issue, instead of directly fixing it, because I'm not sure if there should be some typechecking performed within the except clause or whether to add another try inside the except or - because both feels kind of strange - someone has an even better idea.

Matplotlib version

  • Operating system: Windows 8.1
  • Matplotlib version: 2.2.2
  • Matplotlib backend: any
  • Python version: 3.6
@tacaswell tacaswell modified the milestones: v3.0, v2.2.3 Jun 5, 2018
@tacaswell
Copy link
Member

I also ran into this (but did not take the time to track it down) yesterday.

Running through np.asanyarray is also not great, but I think would be better than explicit type checking. We could also just put the repr of 'c' in the error message but that could get needlessly verbose.

@mwaskom
Copy link

mwaskom commented Jun 5, 2018

Easiest fix would be doing np.shape(c) instead of c.shape, right?

@ImportanceOfBeingErnest
Copy link
Member Author

Maybe it's worth mentioning that the same error is also shown when a nonexisting color is specified, i.e.

import matplotlib.pyplot as plt
x = [1,2,3,4,5]
c = "jaune"
plt.scatter(x,x, c=c)
plt.show()

results in AttributeError: 'str' object has no attribute 'shape'.

When using np.shape(c) instead of c.shape, the error in the above case would be

ValueError: c of shape () not acceptable as a color sequence for x with size 5, y with size 5

which is of course better than what is currently raised, but doesn't really tell the user that the color they used does not exist.

@ImportanceOfBeingErnest
Copy link
Member Author

Given that #11383 is merged, this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants