Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Better error message in scatter plot when len(x) != len(c) #7314

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
NelleV opened this issue Oct 20, 2016 · 2 comments
Closed

Better error message in scatter plot when len(x) != len(c) #7314

NelleV opened this issue Oct 20, 2016 · 2 comments
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
Milestone

Comments

@NelleV
Copy link
Member

NelleV commented Oct 20, 2016

import numpy as np
import matplotlib.pyplot as plt
N = 500
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N+1)

fig, ax = plt.subplots()
ax.scatter(x, y, c=colors)
plt.show()

raises the following error:
ValueError: Invalid RGBA argument: 0.23991184171163127

which is absolutely unrelated to the problem. We need a better error message when the color is not of the correct shape.

@NelleV NelleV added Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Machine Shop labels Oct 20, 2016
@tacaswell tacaswell added this to the 2.1 (next point release) milestone Oct 20, 2016
@anntzer
Copy link
Contributor

anntzer commented Oct 21, 2016

The relevant lines are in the definition of scatter:

        # After this block, c_array will be None unless
        # c is an array for mapping.  The potential ambiguity
        # with a sequence of 3 or 4 numbers is resolved in
        # favor of mapping, not rgb or rgba.
        if c_none or co is not None:
            c_array = None
        else:
            try:
                c_array = np.asanyarray(c, dtype=float)
                if c_array.size == x.size: # <<- here is the interesting line.
                    c = np.ma.ravel(c_array)
                else:
                    # Wrong size; it must not be intended for mapping.
                    c_array = None
            except ValueError:
                # Failed to make a floating-point array; c must be color specs.
                c_array = None

        if c_array is None:
            colors = c     # must be acceptable as PathCollection facecolors
        else:
            colors = None  # use cmap, norm after collection is created

In fact the call to ravel leads to some more funny issues, e.g. the following code "works" when it likely shouldn't:

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(4)
y = np.arange(4)
colors = np.random.rand(2, 2)

fig, ax = plt.subplots()
ax.scatter(x, y, c=colors)
plt.show()

@QuLogic
Copy link
Member

QuLogic commented Nov 16, 2016

Fixed by #7363, I believe.

@QuLogic QuLogic closed this as completed Nov 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
Projects
None yet
Development

No branches or pull requests

4 participants