A though question this time :)
Currently I'm plotting a lot using pylab.imshow()
and/or pylab.contour()
. For illustration purposes I'm including colormaps. However, I'm not very fond of the available colormaps. To this end, I decided to create one using the following code:
import numpy
import matplotlib as mpl
import matplotlib.pylab as plt
def make_cmap(colors):
position = numpy.linspace(0,1,len(colors))
cdict = {'red':[], 'green':[], 'blue':[]}
for pos, color in zip(position, colors):
cdict['red'].append((pos, color[0], color[0]))
cdict['green'].append((pos, color[1], color[1]))
cdict['blue'].append((pos, color[2], color[2]))
cmap = mpl.colors.LinearSegmentedColormap('my_colormap', cdict, 256)
return cmap
colors = [(0, 0, 1), (1, 1, 1), (1, 0, 0)] #cmap from blue to white to red.
plt.figure()
plt.imshow(Data, cmap = make_cmap(colors))
plt.show()
The function works very well. However, what I'm still missing is a centering option. With this option I would like to center the color map around zero. The colormap above ranges from blue to red with white in between.
By default, matplotlib will normalise the colormap such that the maximum colormap value will be the maximum of Data
. However, if the data does not contain any negative values I want to discard the blue part of my colormap as it will be very confusing when comparing different images. Vice versa for red if Data
does not contain positive values.
I know I can set the limits using vmin
and vmax
. However, this is not really what I'm looking for. I would like to include the centering option in my function such that the colormap automatically scales correctly depending on Data
Any ideas?