4

I use the colormap "jet" to plot my graphics. But, I would like to have the lower values in white color and this colormap goes from blue to red colors. I also don't want to use another colormap because I need this range of colors... I tried to make my colormap to get the same as "jet" with a range of values in white, but this is too difficult. Someone could help me please? Thank you

0

3 Answers 3

9

Probably there should be an easiest solution, but the way I figured out is by creating your own matplotlib.colors.LinearSegmentedColormap, based on the "jet" one.

(The lowest level of your colormap is defined in the first line of each tuple of red, green, and blue, so that's where you start editting. I add one extra tuple to have a clearly white spot in lower part. ...for each color, in the first element of the tuple you indicate the position in your colorbar (from 0 to 1), and in the second and third the color itself).

from matplotlib.pyplot import *
import matplotlib
import numpy as np

cdict = {'red': ((0., 1, 1),
                 (0.05, 1, 1),
                 (0.11, 0, 0),
                 (0.66, 1, 1),
                 (0.89, 1, 1),
                 (1, 0.5, 0.5)),
         'green': ((0., 1, 1),
                   (0.05, 1, 1),
                   (0.11, 0, 0),
                   (0.375, 1, 1),
                   (0.64, 1, 1),
                   (0.91, 0, 0),
                   (1, 0, 0)),
         'blue': ((0., 1, 1),
                  (0.05, 1, 1),
                  (0.11, 1, 1),
                  (0.34, 1, 1),
                  (0.65, 0, 0),
                  (1, 0, 0))}

my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)

pcolor(np.random.rand(10,10),cmap=my_cmap)
colorbar()
show()

You'll get the following: jet colormap with white

Sign up to request clarification or add additional context in comments.

Comments

8

There is another way to do the same without defining a new colobar. You can use the cmap.set_under method that defines the color to be used for all values below a given threshold. The threshold is defined during the pcolormesh :

from matplotlib.pyplot import *
import numpy as np

mycmap = cm.get_cmap('jet')
mycmap.set_under('w')

pcolor(np.random.rand(10,10), cmap=mycmap, vmin=.1)
colorbar()
show()

Comments

6

There's pretty much this example on the colormaps page. Digging through the matplotlib source on my machine, jet is defined as (in _cm.py):

_jet_data =   {'red':   ((0., 0, 0), (0.35, 0, 0), (0.66, 1, 1), (0.89,1, 1),
                         (1, 0.5, 0.5)),
               'green': ((0., 0, 0), (0.125,0, 0), (0.375,1, 1), (0.64,1, 1),
                         (0.91,0,0), (1, 0, 0)),
               'blue':  ((0., 0.5, 0.5), (0.11, 1, 1), (0.34, 1, 1), (0.65,0, 0),
                         (1, 0, 0))}

4 Comments

Thank you but how can I add the color white for the lowest level ? I don't quite understand the triple numbers , and where I can find them ? if I want to change the colorbar from the values 0 to 10 for instance, I need to add other triples ?
The lowest level of your colormap is defined in the first line of each tuple of red, green, and blue, where the first element of the tuple represent the values gamma (from 0 to 1) (think about the "position" in a colorbar).
As Mr E wrote, you can find the dictionaries that generate matplotlib colormaps in _cm.py. This file is stored inside your Python folder, usually in the following path: C:\Python##\Lib\site-packages\matplotlib\
For everyone else: You can also just access the segment data with the _segmentdata attribute. So, mpl.cm.jet._segmentdata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.