2014-03-02 156 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) –