Matplotlib scatter color codes when NaN is present
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 dots appear, each with a color. Now some data is missing:
plt.figure()
plt.scatter(x, np.log10(b), color=colors)
This is where the problem arises: there is no data x = 1 (log (0) = NaN), but the colors are missing that point, and the point x = 2 is red, not blue. The solution could be:
y = np.log10(b) mask = np.isfinite(y) plt.scatter(x[mask], y[mask], color=colors[mask])
but I feel like it's so awkward to do it ... in any other way?
+3
source to share
1 answer
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
FROM.
+1
source to share