I'm having problems setting the color of np.nan values in my data set.
I already managed to get the camp.set_bad working in imshow plots but it does not work in plt.scatter.
Anyways, my main goal is assigning a specific colour to bad values.
This is how I though it would work (but it does not ;-)
import matplotlib.pyplot as plt
import numpy as np
n = 20
x = y = np.linspace(1, 10, n)
c = np.random.random_sample((n,))
c[4] = np.nan
c[8:12] = np.nan
cmap = plt.get_cmap('plasma')
cmap.set_bad(color='black', alpha = 1.)
plt.scatter(x, y, c=c, s=200, cmap=cmap)
This gives me the following output:

Of course, I could divide the dataset into two separate sets and overplot them, but I'm quite sure there is a much cleaner solution.
There is no black color in cmap plasma.
Array c has to store indexes of colors which your select from current color map cmap. If your set c as NaN it means you do not get a object for these indices (4 and 8:12) on a scatter-plot.
The first variant is to set color for selected indices manually:
import matplotlib.pyplot as plt
import numpy as np
n = 20
x = y = np.linspace(1, 10, n)
c = np.random.random_sample((n,))
#c[4] = np.nan
#c[8:12] = np.nan
c[4]=c[8:12]=0 # first color use to mark 4 and 8:12 elements
cmap = plt.get_cmap('plasma')
plt.scatter(x, y, s=200, c=c, cmap=cmap)
plt.show()

The second variant is to draw two scatter-plots:
import matplotlib.pyplot as plt
import numpy as np
n = 20
x = y = np.linspace(1, 10, n)
c = np.random.random_sample((n,))
c[4] = np.nan
c[8:12] = np.nan
cmap = plt.get_cmap('plasma')
# plot good values
indices = ~np.isnan(c)
plt.scatter(x[indices], y[indices], s=200, c=c[indices], cmap=cmap)
# plot bad values
plt.scatter(x[~indices], y[~indices], s=200, c='k')
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With