Skip to content

matplotlib scatter shifts color codes when NaN is present #3489

Closed

Description

@Morisset

I want to plot some data with a given color code. For example:

x = np.array([0, 1, 2, 3])
b = np.array([1, 0, 2, 3])
colors = np.array(['g', 'r', 'b', 'y'])
plt.scatter(x, b**2, color=colors)

Great, all the points appear, each one with its color. Now, some data is missing:

plt.figure()
plt.scatter(x, np.log10(b), color=colors)

Here comes the problem: the x=1 data is missing (log(0)=NaN), but the colors are skipping this point and the x=2 point is red, not blue. The solution can be:

y = np.log10(b)
mask = np.isfinite(y)
plt.scatter(x[mask], y[mask], color=colors[mask])

but I feel it so uncomfortable to do this... Any other way? Is this a kind of bug?
Thanks,
Christophe

Activity

  1. added this to the v1.4.x milestone on Sep 9, 2014
  2. added
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    on Sep 9, 2014
  3. efiring commented on Sep 10, 2014

    @efiring
    Member

    Yes, it's a bug; but there is a function call in scatter that is supposed to be handling exactly this situation, and I don't see immediately why it is failing to do so in this instance. It looks like calling the function, cbook.delete_masked_points(), directly with the arguments works as intended, so it appears that something is getting fouled up prior to the function call.

  4. added
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    and removed
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    on Sep 10, 2014
  5. efiring commented on Sep 10, 2014

    @efiring
    Member

    Aha! @tacaswell was right--the problem was the API. The correct kwarg is "c", not "colors". I don't know what to do about that; maybe pop "colors" off the kwarg list and use it as a synonym for "c"?

  6. tacaswell commented on Sep 10, 2014

    @tacaswell
    Member

    Right by dumb luck!

    And that sounds reasonable to me use color as an alias for c.

    In fact right now you can pass in both c and color and in some cases it looks like color will over-ride c.

    It also looks like facecolor, facecolors, edgecolor and edgecolors will all also have similar problems.

    [edit to finish sentence]

  7. WeatherGod commented on Sep 10, 2014

    @WeatherGod
    Member

    All the more reason for some sort of kwarg normalization function discussed
    yesterday...

    On Tue, Sep 9, 2014 at 11:01 PM, Thomas A Caswell notifications@github.com
    wrote:

    Right by dumb luck!

    And that sounds reasonable to me In fact right now you can pass in both c
    and color and in some cases it looks like color will over-ride c.

    It also looks like facecolor, facecolors, edgecolor and edgecolors will
    all also have similar problems.

    —
    Reply to this email directly or view it on GitHub
    #3489 (comment)
    .

  8. modified the milestones: , v1.4.x on Oct 17, 2014
  9. modified the milestones: v1.4.3, v1.4.x on Jan 27, 2015
  10. 3 remaining items

  11. modified the milestones: , v1.4.x on Feb 7, 2015
  12. modified the milestones: , on Aug 9, 2015
  13. story645 commented on Jun 1, 2016

    @story645
    Member

    does #4079 also fix this one?

  14. efiring commented on Jun 1, 2016

    @efiring
    Member

    @story645 I suspect it does, possibly in conjunction with later changes. Would you test v2.x with the example given above, please? If you find that the bug no longer appears, I will close this issue.

  15. self-assigned this
    on Jun 1, 2016
  16. removed their assignment
    on Dec 5, 2016
  17. story645 commented on Dec 5, 2016

    @story645
    Member

    Sorry had totally spaced out on this. Tested and still broken, but now there's #7570

  18. tacaswell commented on Dec 5, 2016

    @tacaswell
    Member

    @story645 Could you review #7570 ?

  19. modified the milestones: , on Dec 5, 2016
  20. modified the milestones: , on Dec 11, 2016
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

    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesmentored: hackathon

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions