Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorplot that distinguishes between positive and negative values

As one can see in this sample code since 0 is somewhere in the spectrum it is hard to trace which points are negative and which are positive. Although my real plot is more contiguous I wonder if there is a way to seperate negative and postivie values in these clorplots; for example how can I use two different spectrum of colours for positive and negative values.

import numpy as np
from matplotlib import pyplot as plt
a=np.random.randn(2500).reshape((50,50))
plt.imshow(a,interpolation='none')
plt.colorbar()
plt.show()

enter image description here

EDIT With the help of @MultiVAC and looking for solutions I came across this.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import BoundaryNorm
a=np.random.randn(2500).reshape((50,50))

# define the colormap
cmap = plt.cm.jet
# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# create the new map
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)

# define the bins and normalize
bounds = np.linspace(np.min(a),np.max(a),5)
norm = BoundaryNorm(bounds, cmap.N)

plt.imshow(a,interpolation='none',norm=norm,cmap=cmap)
plt.colorbar()
plt.show()

Still I don't know how to differentiate zero!

enter image description here

like image 381
Cupitor Avatar asked Jun 02 '14 11:06

Cupitor


2 Answers

Ok for the future reference. I used diverging maps as part of it as @tcaswell suggested. You can look to the above links.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import BoundaryNorm
a=np.random.randn(2500).reshape((50,50))

# define the colormap
cmap = plt.get_cmap('PuOr')

# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# create the new map
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)

# define the bins and normalize and forcing 0 to be part of the colorbar!
bounds = np.arange(np.min(a),np.max(a),.5)
idx=np.searchsorted(bounds,0)
bounds=np.insert(bounds,idx,0)
norm = BoundaryNorm(bounds, cmap.N)

plt.imshow(a,interpolation='none',norm=norm,cmap=cmap)
plt.colorbar()
plt.show()

enter image description here

like image 183
Cupitor Avatar answered Oct 28 '22 12:10

Cupitor


I arrived at this thread looking for something like what I've written below, hopefully others find it helpful.

import matplotlib.colors as colors
from matplotlib import cm
import numpy as np
import seaborn as sns

with sns.axes_style('whitegrid'):
    rand_normal_y = np.random.randn(1000)
    x = np.arange(0,1000, 1)
    norm = colors.CenteredNorm()
    rand_normal_y_norm = norm(rand_normal_y)
    cmap = cm.coolwarm(rand_normal_y_norm)
    sns.scatterplot(x = x, y = rand_normal_y , c=cmap,  )
    plt.plot(np.linspace(0,1000, 1000), np.repeat(0, 1000), color = 'black', ls = "-")

enter image description here

like image 25
Kelley Brady Avatar answered Oct 28 '22 13:10

Kelley Brady