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

Issue when setting scatter color in separate method call · Issue #10381 · matplotlib/matplotlib · GitHub

Repository navigation

Issue when setting scatter color in separate method call #10381

Closed

Description

For an application I am developing, I need to call ax.scatter then change the colors of the points after the fact. However, if the input x/y values contain NaN values, then setting the colors after the fact doesn't work properly. The following example:

import matplotlib
matplotlib.use('Agg')

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, aspect='auto')

x = np.array([1, 2, 3])
y = np.array([2, np.nan, 4])
c = np.array([3, 4, 5])

s = ax.scatter(x, y, c=c, vmin=3, vmax=5, s=320, zorder=1, edgecolor='none')

s = ax.scatter(x, y, s=160, zorder=2, edgecolor='none')
s.set_array(c)
s.set_clim(3, 5)

s = ax.scatter([], [], s=40, zorder=3, edgecolor='none')
s.set_offsets(np.vstack([x, y]).transpose())
s.set_array(c)
s.set_clim(3, 5)

fig.savefig('test.png')

produces the following output:

The top right point should be all yellow. However it isn't, because in the scatter call, invalid values are removed, so when I try and set the colors with set_array, the color values are offset from the x/y values. In the final case, things work since the 'bad' values are masked from neither the offsets nor the colors.

Is there a way to achieve something along the lines of the middle example (setting the colors after the fact)

Activity

  1. tacaswell commented on Feb 5, 2018

    Member

    This is because we create the initial artist and strip out any points that have nan in them. The new colors will then be offset from where you think they should be.

    attn @efiring as you have been working around this code recently.

  2. added this to the milestone on Feb 5, 2018
  3. efiring commented on Feb 5, 2018

    Member

    I think that adding the requested functionality will require writing a subclass of PathCollection that can keep track of the original inputs, and can repeat the mask- and nan-filtering operation every time a relevant property is updated.

  4. anntzer commented on Feb 6, 2018

    Contributor

    Do we actually need to filter out the nans (except for backcompatibility considerations)? Aren't the renderers supposed to be able to handle nan data to start with anyways?

  5. efiring commented on Feb 7, 2018

    Member

    Good question; maybe they all can. If so, taking advantage of that would require some substantial changes, I think, because we support masked arrays, and use them internally. Going 100% with nans for missing values implies always using floating point, and switching from .max to .nanmax etc.

  6. modified the milestones: v3.0, v3.1 on Aug 11, 2018
  7. modified the milestones: v3.1.0, v3.2.0 on Mar 18, 2019
  8. efiring commented on Apr 15, 2019

    Member

    I think this is closed by #12422. If not, please reopen.

  9. modified the milestones: v3.2.0, v3.1.0 on Apr 15, 2019
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL