Ask Your Question

# Custom color map for matrix plot?

I need colormap which has for a fixed value x the color white and all values above x are colorized from white do blue and all values below x are colorized from white to red.

The first question is, is this possible? And my second question how can I do that?

edit retag close merge delete

## 2 answers

Sort by ยป oldest newest most voted

You'll probably have to make a custom colormap and then pass that right in to matrix_plot, perhaps using ListedColormap or LinearSegmentedColormap. But I think this should be doable, as we use matplotlib colormaps as they stand.

more

## Comments

I try this thanks

( 2014-09-08 12:12:30 -0600 )edit

@kcrisman is absolutly right:

here are some code examples based on that articel Custom Colormaps

With a lookup dict:

from pylab import *
cdict = {'red': ((0.0, 0.0, 0.0),
(0.5, 1.0, 0.7),
(1.0, 1.0, 1.0)),
'green': ((0.0, 0.0, 0.0),
(0.5, 1.0, 0.0),
(1.0, 1.0, 1.0)),
'blue': ((0.0, 0.0, 0.0),
(0.5, 1.0, 0.0),
(1.0, 0.5, 1.0))}
my_cmap = matplotlib.colors.LinearSegmentedColormap( 'my_colormap', cdict, 256 )
matrix_plot( matrix( [ [1,3,5,1], [2,4,5,6], [1,-3,5,7] ] ), cmap = my_cmap, colorbar = True )


From pool:

from pylab import *
N = 8
cpool = [ '#bd2309', '#bbb12d', '#1480fa', '#14fa2f', '#000000',
'#faf214', '#2edfea', '#ea2ec4', '#ea2e40', '#cdcdcd',
'#577a4d', '#2e46c0', '#f59422', '#219774', '#8086d9' ]
cmap3 = matplotlib.colors.ListedColormap(cpool[0:N], 'indexed')
matrix_plot( matrix( [ [1,3,5,1], [2,4,5,6], [1,-3,5,7] ] ), cmap = cmap3, colorbar = True )


As gradient:

from pylab import *
startcolor = '#FF0000'
midcolor = '#FFFFFF'
endcolor = '#0030CC'
own_cmap1 = matplotlib.colors.LinearSegmentedColormap.from_list( 'own2', [startcolor, midcolor, endcolor] )
matrix_plot( matrix( [ [1,3,5,1], [2,4,5,6], [1,-3,5,7] ] ), cmap = own_cmap1, colorbar = True )

more

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2014-09-08 10:27:10 -0600

Seen: 556 times

Last updated: Sep 19 '14