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()
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!
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()
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 = "-")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With