FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Better error message in scatter plot when len(x) != len(c) · Issue #7314 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

Description

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.

Activity

  1. added this to the milestone on Oct 20, 2016
  2. anntzer commented on Oct 21, 2016

    Contributor

    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()
    
  3. QuLogic commented on Nov 16, 2016

    Member

    Fixed by #7363, I believe.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL