Skip to content

scatter() should not drop data points at nonfinite coordinates #12801

Closed

Description

@anntzer

Currently, scatter() completely drops data points at nonfinite coordinates from the PathCollection it builds:

fig, ax = plt.subplots()
x = [0, 1, np.nan, 3]
y = [0, 1, 2, 3]
c = [0, 1, 2, 3]
pc = ax.scatter(x, y, c=c)
print(pc.get_offsets())

results in

[[0. 0.]
 [1. 1.]
 [3. 3.]]

i.e. the [2, nan] point is not recorded anywhere, even though we could just as well keep track of it and let the renderer do the job of not rendering nonfinite points.

As a result, this makes event picking less-than-optimal: adding

fig.canvas.mpl_connect("button_press_event", lambda event: print(pc.contains(event)))

to the code above, one sees that the first point is picked at index 0 ((True, {'ind': array([0], dtype=int32)}) is printed when clicking on it), the second one at index 1, and the last one at index 2. So one cannot use the resulting index to lookup into x/y/c, one has to index into pc.get_offsets()/pc.get_array(); and if one wants to index into some other array (my actual use case), one needs to manually count the nans in x/y/c and do the index conversion.
As a comparison point, Line2D does keep invalid points around and lets the renderer kill them.

Likewise, points with nonfinite values are dropped:

pc = ax.scatter([0, 1, 2, 3], [0, 1, 2, 3], c=[0, np.nan, np.inf, 1])

results in behavior similar to the one described above.
In this specific case, I actually think nonfinite points should respect the colormap's colors set by set_under/set_over/set_bad (right now they don't, they are again wiped out of existence) -- after all that's exactly what these "extra" colors are for.

This second issue (invalid c) has already been reported elsewhere and there are already PRs for it: #4354/#9891/#12422.

mpl master/3.0.2
Arch Linux/Py3.7

Activity

  1. tgbrooks commented on Dec 11, 2018

    @tgbrooks

    Just ran into this problem too (specifically for using a picker), which was particularly difficult to track down since it worked on the low indexes (before any nan's), which were the ones that were easiest to double check by hand.

  2. added this to the v3.1 milestone on Dec 11, 2018
  3. added
    API: changesChanges to the public API, typically requiring deprecation.
    on Dec 11, 2018
  4. anntzer commented on Dec 17, 2018

    @anntzer
    ContributorAuthor

    Essentially closed by #12422, I think.

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: changesChanges to the public API, typically requiring deprecation.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions