Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib hist2d colormap for null pixels

I found the general approach to set the null pixels to white is to use a lognorm colormap.

Is there a way to use a linear colormap but still set null pixels to white?

Thanks!

like image 916
frankliuao Avatar asked Nov 04 '16 11:11

frankliuao


2 Answers

In new versions of matplotlib you can simply use cmin = 1, i.e.

plt.hist2d(x,y,cmin = 1)

will set all bins with fewer than one count to nan, i.e. nonvisible. If your background is white (default) then these bins will appear white. But they will change wil the background.

like image 191
Jürg Merlin Spaak Avatar answered Sep 22 '22 12:09

Jürg Merlin Spaak


You can use '.set_under':

import matplotlib.pyplot as plt

my_cmap = plt.cm.jet
my_cmap.set_under('w',1)
...
plt.hist2d( ..., cmap = my_cmap)

This sets all values under 1 (i.e. the lowest count) to 'w' (=white).

like image 32
Haminaa Avatar answered Sep 24 '22 12:09

Haminaa