Here are two ways to do this. One of them created its own colormap
, and the other using masked array
. Say we have:
import matplotlib from pylab import * data = np.arange(-50, 50).reshape(10, 10) data = np.abs(data) pcolor(data, cmap=cm.YlOrRd) show()
This gives:
Now we do the same, but create a list called colors
that has the same values โโas cm.YlOrRd
, except for the entry 0
, which we set to black ( 0,0,0
in rgb). Then we use LinearSegmentedColormap.from_list
to actually create the color map:
import matplotlib from pylab import * data = np.arange(-50, 50).reshape(10, 10) data = np.abs(data) colors = [(0,0,0)] + [(cm.YlOrRd(i)) for i in xrange(1,256)] new_map = matplotlib.colors.LinearSegmentedColormap.from_list('new_map', colors, N=256) pcolor(data, cmap=new_map) savefig('map.png') show()
This gives the same plot, but zero values โโare black:
Another way to use masked arrays, its a bit more involved, comments in the code explain the steps:
from pylab import * import numpy.ma as ma data=np.arange(-50,50).reshape(10,10) data=np.abs(data)
Gives the same schedule as above.
There is a potential difference between these methods, the first method rounds up the data values โโand applies the corresponding color, while the second method sets the values โโequal to 0 to black (that is, 0.001 will not be masked, be the appropriate color cm.YlOrRd
). The main advantage of the second is that you can mask recordings completely arbitrarily.