X Tutup
Skip to content

matplotlib scatter shifts color codes when NaN is present #3489

@Morisset

Description

@Morisset

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? Is this a kind of bug?
Thanks,
Christophe

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup