Improper handling of color in set_color when data contains NaN. · Issue #9891 · matplotlib/matplotlib · GitHub
Skip to content

Improper handling of color in set_color when data contains NaN. #9891

Description

@amicitas

Bug report

Bug summary

When a scatter plot is made with data that contains np.nan (NaN), and then a color array is given using set_color, the wrong colors get mapped to the data points.

Code for reproduction

from matplotlib import pyplot
from matplotlib.colors import Normalize
from matplotlib import cm
import numpy as np

x = np.linspace(0.0, 1.0, 50)
y = np.linspace(0.0, 1.0, 50)
norm = Normalize(0.0, 1.0)
colormap = cm.ScalarMappable(norm, 'gnuplot')
colors = colormap.to_rgba(y)

# Expected colors with when x and y do not contain any np.nan.
# Success.
plot = pyplot.scatter(x, y, color=colors)
pyplot.show()

# Add some nan values.  
# If the color array is given during the initial creation of the
# scatter plot, then everything works as expected.
# Success.
x[10:40] = np.nan
plot = pyplot.scatter(x, y, color=colors)
pyplot.show()

# Again add some nan values.  
# If the color array is given as a call to set_color
# then the colors no longer match the data.
# Failure.
x[10:40] = np.nan
plot = pyplot.scatter(x, y)
plot.set_color(colors)
pyplot.show()

Actual outcome

Top: Expected colors with when x and y do not contain any np.nan.

Middle: Add some nan values. If the color array is given during the initial creation of the scatter plot, then everything works as expected.

Bottom: Again add some nan values. If the color array is given as a call to set_color then the colors no longer match the data.

screen shot 2017-11-30 at 15 19 02

Expected outcome

The colors corresponding to the data points should be displayed, even when data points contain NaN and set_color is used.

Matplotlib version

  • Operating system: OSX
  • Matplotlib version: 2.0.2
  • Matplotlib backend: module://ipykernel.pylab.backend_inline & MacOSX
  • Python version: 3.6.3
  • Jupyter Notebook version: 5.2.0
  • Other libraries: None

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions