SH | Diensttagebuch

In the middle of the desert you can say anything you want

05 Jan 2021

Day 735

matplotlib pyplot make certain color transparent

For this, a subset has to become bad values, and a cmap has to set what to do with them.

my_cmap = copy.copy(plt.cm.get_cmap('gray')) # get a copy of the gray color map
my_cmap.set_bad(alpha=0) # set how the colormap handles 'bad' values
plt.imshow(thing, cmap=my_cmap)

1

As for bad values, I wrote a function similar to this to make them arbitrary:

def get_bad_values(matr, value=0):
	new_matr = matr.astype(np.float)
	new_matr[new_matr == value] = np.nan
	return new_matr

Note that np.nan can only replace a np.float, never an int!


  1. Making image white space transparent, overlay onto imshow() - CrazyGeeks ↩︎

Nel mezzo del deserto posso dire tutto quello che voglio.