22

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

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Marcos Alex
  • 1,600
  • 3
  • 15
  • 20

1 Answers1

45

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()

enter image description here

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()

enter image description here

gg349
  • 21,996
  • 5
  • 54
  • 64
  • Yes, I do need two different colormaps. I wonder if by following this approach it is possible to plot only one palette for the both colormaps. – Marcos Alex Mar 02 '14 at 15:28
  • 1
    The both palettes can be put together (one above other) if we put them in grids. [Here](http://stackoverflow.com/questions/19407950/how-to-align-vertically-two-or-more-plots-x-axis-in-python-matplotlip-provid) is an example. To achieve what I asked above, we must create [inner grids](http://matplotlib.org/users/gridspec.html#gridspec-using-subplotspec) – Marcos Alex Mar 05 '14 at 16:26
  • You can also create a custom colormap that joins the two colormaps you used, like so: https://stackoverflow.com/a/31052741/9842472 – Puff Oct 14 '22 at 19:56