2014-03-02 217 views
11

讓我們假設下面兩種不同顏色的colormaps matplotlib

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

我想在不同的顏色表比上述零

+0

我想你」必須製作自己的色彩地圖。 – daveydave400

+0

相關:http://stackoverflow.com/questions/31051488/combining-two-matplotlib-colormaps – binaryfunt

回答

21

首先的值,顯示低於零值的例子,是有可能你只是想使用一個發散的顏色映射,在零點處爲「中性」,並且發散爲兩種不同的顏色?這是一個例子:

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

如果你真的想使用兩種不同的色彩映射表,這是蒙面陣列的解決方案:

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

+0

是的,我需要兩個不同的顏色映射。我想知道是否通過遵循這種方法,可以爲這兩種顏色圖僅繪製一個調色板。 –

+0

如果我們把它們放在網格中,兩個調色板可以放在一起(一個在另一個之上)。 [Here](http://stackoverflow.com/questions/19407950/how-to-align-vertically-two-or-more-plots-x-axis-in-python-matplotlip-provid)就是一個例子。爲了實現我上面提到的問題,我們必須創建[內部網格](http://matplotlib.org/users/gridspec.html#gridspec-using-subplotspec) –