2017-02-22 99 views
1

我正在绘制matplotlib中的几个热图,如下所示。matplotlib中的自定义pcolor

enter image description here

这里是我的循环:

with open(gene_peak) as f: 
    count = 1 
    for line in f: 
     np_array=[] 
     gene_peak = line.strip().split("\t") 
     gene_id = gene_peak[0] 
     peaks = gene_peak[1].split(",") 
     for peak in peaks: 
      np_array.append(enhancer_fc[peak]) 
     data, pval = stats.spearmanr(np.transpose(np.array(np_array))) 
     plt.subplot(4,3,count+1) 
#  plt.title(gene_id) 
     plt.pcolor(data, cmap=plt.cm.OrRd, vmin=-1, vmax=1) 
     plt.gca().invert_yaxis() 
     plt.gca().set_aspect(aspect='equal', adjustable='box-forced') 
     plt.xticks([]) 
     plt.yticks([]) 
     print count 
     count += 1 
    plt.show() 

我绘制不同尺寸的不同的二维数组的Spearman相关。

问题:

有相关值,所以它们的范围从-1到1。我要添加定制彩条(),使得上面0.4开始值表示的红色和低于-0.4的梯度表示的梯度的蓝色,这样我只显示大于0.4且小于-0.4的点。

enter image description here

此外,我想绘制只有一个彩条(),使得图像看起来更干净。任何帮助将不胜感激,谢谢。

回答

0

您可以使用Matplotlib的ListedColorMap来定义自己的离散色彩映射表。您可以使用其中一个绘图中的色条,并将其置于适当的位置,以便可视化地表示所有绘图。以下是您给出颜色的示例:

from matplotlib import colors 


discrete_colors = [(255, 0, 20), (255, 70, 65), (255, 128, 110), (255, 181, 165), (64, 64, 64), 
    (0, 0, 0), (64, 64, 64), (124, 128, 217), (102, 107, 216), (69, 76, 215), (33, 33, 245)] 
discrete_colors = [(r/255., g/255., b/255.) for r, g, b in discrete_colors]   

my_colormap = colors.ListedColormap(discrete_colors) 

subplot(211) 
data = 2 * np.random.rand(10, 10) - 1.0 
pcolor(data, cmap=my_colormap, vmin=-1, vmax=1) 
subplot(212) # Some other plot 
data = 2 * np.random.rand(10, 10) - 1.0 
pc = pcolor(data, cmap=my_colormap, vmin=-1, vmax=1) 

fig = gcf() 
fig.subplots_adjust(right=0.70) 
cax = fig.add_axes([0.80, 0.15, 0.05, 0.7]) 
fig.colorbar(pc, cax=cax) 

您可能需要调整一下代码。我正在使用IPython 2.7。 Custom discrete colormap and colorbar placement.