rahul saxena
rahul saxena

Reputation: 119

add white color to pylab colorbar default colormap

Hi I have the following code:

rateMap, occMap, spikeMap = pickle.load(open(rateMapFileName))

pl.imshow(rateMap)
pl.colorbar()

Now occMap is the occupancy map containing x,y position at multiple timestamps. There are some points x,y locations which have no occupancy (i.e. = 0 value at that matrix value)

When I plot rateMap(which is = spikeMap/occMap) using the script above. The colorbar that is generated uses default jetmap colormap. I want the null/0 values from occMap to appear as white in the rateMap plot. So anyway to add zero to colorbar function? and also relating it with the occMap

I tried looking at this script How to add "black" to matplotlib colormap? but couldn't get it to work?

Here is an example image which has white spots basically referring to the x,y location with 0 value or no occupancy:

enter image description here

*****EDIT**** I tried playing around with the code, here is how I was added white color to the colormap but I want to add this white color to default jet colormap. Any suggestions??

cmap = LinearSegmentedColormap.from_list('mycmap', ['white', 'blue', 'cyan', 'green', 'yellow','red'])
fig, ax = plt.subplots()
im = ax.imshow(im, cmap=cmap, interpolation='nearest')
fig.colorbar(im)
plt.show()

Upvotes: 1

Views: 8442

Answers (1)

Nick Zhang
Nick Zhang

Reputation: 709

For your problem, I suggest setting the first level to be greater than zero, and using the extend feature of colorbar to specify zero x, y locations.

First we import libs and try to generate some random data from numpy's normal distribution function on a sparse grid.

from copy import copy

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors

# generate sample data
x0, x1 = -5, 5
y0, y1 = -3, 3
x = np.linspace(x0, x1, 60)
y = np.linspace(y0, y1, 60)
X, Y = np.meshgrid(x, y)
Z = np.random.randn(60, 60)

To illustrate your problem, we set all negative values to zero, rendering non-negative grid data Z.

Z = np.where(Z > 0, Z, 0)

Now comes to the key point! Simply add a out-of-range color to a colormap.

palette = copy(plt.get_cmap('viridis_r'))
palette.set_under('white', 1.0)  # 1.0 represents not transparent

You may also use set_over, set_bad methods to set color for over-range data and undefined (NaN) data.

Set subplots:

fig, ax = plt.subplots(1, 1)

Set levels for plot, but do remember to modify the lowest level to a positive number close to zero.

levels = np.arange(0, 3.5, 0.5)
levels[0] = 1e-5

Here's how to plot the array Z.

norm = colors.BoundaryNorm(levels, ncolors=palette.N)
im = ax.imshow(Z, cmap=palette,
               norm=norm,
               aspect='auto', extent=[x0, x1, y0, y1])
# Possible extend options include: ['min', 'max', 'both', 'neither']
cbar = fig.colorbar(im, extend='min', shrink=0.9, ax=ax)
plt.savefig('draw.png', dpi=300)
# or
# plt.show()

draw.png

Displaying the lowest level as 0.0 may lead to some ambiguity, perhaps setting levels[0] to 0.1 look better to you.

You may refer to following links for more:

https://matplotlib.org/examples/pylab_examples/image_masked.html

https://matplotlib.org/examples/pylab_examples/contourf_demo.html

Upvotes: 4

Related Questions