4

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?

StarObs
  • 101
  • 3

1 Answers1

2

Using c instead of color solves the problem:

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

Thanks to the github people: https://github.com/matplotlib/matplotlib/issues/3489

C.

StarObs
  • 101
  • 3
  • 1
    Remember to accept your own answer when the system will let you. – tacaswell Sep 10 '14 at 13:39
  • This is a old question, but I just got here for the same problem. It's worth noting that this correction does not work for older version of matplotlib. (I'm on a system using 2.0.0 for whatever reason, even though that's no longer current.) – Brick Sep 25 '19 at 15:03