0

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?

The Dude
  • 3,795
  • 5
  • 29
  • 47
  • 1
    I think you want to do something like this: http://nbviewer.ipython.org/gist/phobson/7916777 – mwaskom Mar 31 '14 at 02:17
  • 1
    Actually, I think what you want is a custom `Normalize`, rather than a custom colormap. – Joe Kington Mar 31 '14 at 18:29
  • I've been looking into `Normalize` and it is indeed what I need. I actually found a similar question which was answered by you Joe: [link](http://stackoverflow.com/questions/20144529/shifted-colorbar-matplotlib). However, if I have multiple colorbars all with different vmin and vmax I need to redefine norm for all of them. If I do not do this, the first norm which sets the range from 0 to 1 is applied to all the colorbars. Even the once that range from -10 to 1. Is there a work around to redefining norm for all of them? – The Dude Mar 31 '14 at 22:27

0 Answers0