Let's suppose the example below
import matplotlib.pyplot as plt
import numpy as np
v1 = -1 + 2*np.random.rand(50,150)
fig = plt.figure()
ax = fig.add_subplot(111)
p = ax.imshow(v1,interpolation='nearest')
cb = plt.colorbar(p,shrink=0.5)
plt.xlabel('Day')
plt.ylabel('Depth')
cb.set_label('RWU')
plt.show()
I want to show the values below zero in a different colormap than the values above zero
The most direct way is to just render your array to RGB using the colormap, and then change the pixels you want.
( cmaps.viridis is a matplotlib.colors.ListedColormap ) import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np import colormaps as cmaps img=mpimg.imread('stinkbug.png') lum_img = np.flipud(img[:,:,0]) imgplot = plt.pcolormesh(lum_img, cmap=cmaps.viridis)
Just specify vmin=0, vmax=1 . By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).
First of all, is it possible that you just want to use a diverging colormap, 'neutral' at zero, and diverging to two distinct colours? This is an example:
import matplotlib.pyplot as plt
import numpy as np
v1 = -1+2*np.random.rand(50,150)
fig,ax = plt.subplots()
p = ax.imshow(v1,interpolation='nearest',cmap=plt.cm.RdBu)
cb = plt.colorbar(p,shrink=0.5)
ax.set_xlabel('Day')
ax.set_ylabel('Depth')
cb.set_label('RWU')
plt.show()
If you really want to use two different colormaps, this is a solution with masked arrays:
import matplotlib.pyplot as plt
import numpy as np
from numpy.ma import masked_array
v1 = -1+2*np.random.rand(50,150)
v1a = masked_array(v1,v1<0)
v1b = masked_array(v1,v1>=0)
fig,ax = plt.subplots()
pa = ax.imshow(v1a,interpolation='nearest',cmap=cm.Reds)
cba = plt.colorbar(pa,shrink=0.25)
pb = ax.imshow(v1b,interpolation='nearest',cmap=cm.winter)
cbb = plt.colorbar(pb,shrink=0.25)
plt.xlabel('Day')
plt.ylabel('Depth')
cba.set_label('positive')
cbb.set_label('negative')
plt.show()
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