2016-03-01 114 views
3

我目前有两个大型数据集,我想对它们进行比较。我分别有他们,一个红色和一个蓝色,但我想显示红色和蓝色并排。我该怎么办?使用两种不同颜色的数据集创建matplotlib热图

我当前的代码是:

column_labels = list(heatmap_ylabels) 
row_labels = list(heatmap_xlabels) 
fig, ax = plt.subplots() 
heatmap = ax.pcolor(data, cmap=plt.cm.Reds) 

ax.set_xticks(np.arange(9+0.5)) 
ax.set_yticks(np.arange(140+0.5)) 

ax.invert_yaxis() 
ax.xaxis.tick_top() 
ax.set_xticklabels(row_labels, minor=False) 
ax.set_yticklabels(column_labels, minor=False) 
#plt.show() 
plt.savefig('n1_heatmap') 
plt.clf() 

column_labels = list(heatmap_ylabels) 
row_labels = list(heatmap_xlabels) 
fig, ax = plt.subplots() 
heatmap = ax.pcolor(data1, cmap=plt.cm.Blues) 

ax.set_xticks(np.arange(9+0.5)) 
ax.set_yticks(np.arange(140+0.5)) 

ax.invert_yaxis() 
ax.xaxis.tick_top() 
ax.set_xticklabels(row_labels, minor=False) 
ax.set_yticklabels(column_labels, minor=False) 
plt.savefig('n2_heatmap') 
plt.clf() 

两个datadata1是来自280个不同的文件中提取信息140名不同的列表形成的,是有办法,我仍然可以以创建一个使用这两个列表将在同一图中显示这些数据的热图?

因此,例如,我的热图会/红/蓝/红/蓝等

这里是我的热图的一个例子:

enter image description here

编辑:

虽然不显示正是我想要的,我已经制作了两幅以前热度图之间数值差异的热图。

如:y2 = np.subtract(y, y1)

data2.append(y2) 
column_labels = list(heatmap_ylabels) 
row_labels = list(heatmap_xlabels) 
fig, ax = plt.subplots() 
heatmap = ax.pcolor(data2, cmap=plt.cm.bwr) 

ax.set_xticks(np.arange(9+0.5)) 
ax.set_yticks(np.arange(140+0.5)) 

ax.invert_yaxis() 
ax.xaxis.tick_top() 
ax.set_xticklabels(row_labels, minor=False) 
ax.set_yticklabels(column_labels, minor=False) 
plt.savefig('diff_heatmap') 
plt.clf() 
+0

当你说'同时显示红色和蓝色'你的意思是保持一切都一样,但只是让它两个数字出现在一起?或者你的意思是在同一个数字上绘制红色数据和蓝色数据? – DavidG

+0

我想将红色和蓝色的数据绘制在同一张图上进行比较,所以列会变成红色/蓝色/红色/蓝色等。 – Charlietrypsin

+0

[绘制2D数据:使用不同颜色映射的热图](http:/ /stackoverflow.com/questions/25154056/plotting-of-2d-data-heatmap-with-different-colormaps) – jrjc

回答

2

正如@jeanrjc提到的,这是概念上非常类似于a previously-asked question。但是,如何在您的案例中应用该方法可能并不明显。

下面是使用两个不同颜色映射以相同形状“并排”绘制两个阵列的最小示例。关键是独立绘制两个屏蔽阵列。要创建这些被屏蔽的数组,我们将创建具有两倍列数的新数组,并屏蔽每一列。

下面是一个简单的例子(注意,有几种方法来创建蒙面阵列模式):

import numpy as np 
import matplotlib.pyplot as plt 

# Generate data 
nrows, ncols = 20, 5 
x = np.random.random((nrows, ncols)) 
y = np.random.random((nrows, ncols)) 

# Make data for display 
mask = np.array(nrows * [ncols * [False, True]], dtype=bool) 
red = np.ma.masked_where(mask, np.repeat(x, 2, axis=1)) 

mask = np.array(nrows * [ncols * [True, False]], dtype=bool) 
blue = np.ma.masked_where(mask, np.repeat(y, 2, axis=1)) 

# Make a side-by-side plot 
fig, ax = plt.subplots() 
ax.pcolormesh(red, cmap='Reds') 
ax.pcolormesh(blue, cmap='Blues') 
plt.show() 

enter image description here

如果我们想要做票友的版本,我们可以做同样的事情于:

import numpy as np 
import matplotlib.pyplot as plt 

# Generate data 
nrows, ncols = 20, 5 
x = np.exp(np.random.normal(0, 0.8, (nrows, ncols))) 
y = np.exp(np.random.normal(0, 1, (nrows, ncols))) 

# Make data for display 
mask = np.array(nrows * [ncols * [False, True]], dtype=bool) 
red = np.ma.masked_where(mask, np.repeat(x, 2, axis=1)) 

mask = np.array(nrows * [ncols * [True, False]], dtype=bool) 
blue = np.ma.masked_where(mask, np.repeat(y, 2, axis=1)) 

# Make a side-by-side plot 
fig, ax = plt.subplots() 
redmesh = ax.pcolormesh(red, cmap='Reds') 
bluemesh = ax.pcolormesh(blue, cmap='Blues') 

# Make things a touch fancier 
ax.set(xticks=np.arange(1, 2 * ncols, 2), 
     yticks=np.arange(nrows) + 0.5, 
     xticklabels=['Column ' + letter for letter in 'ABCDE'], 
     yticklabels=['Row {}'.format(i+1) for i in range(nrows)]) 

ax.set_title('Side-by-Side Plot', y=1.07) 
ax.xaxis.tick_top() 
ax.yaxis.tick_left() 
ax.tick_params(direction='out') 

# Add dual colorbars 
fig.subplots_adjust(bottom=0.05, right=0.78, top=0.88) 
cbar = fig.colorbar(redmesh, cax=fig.add_axes([0.81, 0.05, 0.04, 0.83])) 
cbar.ax.text(0.55, 0.1, 'Variable 1', rotation=90, ha='center', va='center', 
      transform=cbar.ax.transAxes, color='gray') 
cbar = fig.colorbar(bluemesh, cax=fig.add_axes([0.9, 0.05, 0.04, 0.83])) 
cbar.ax.text(0.55, 0.1, 'Variable 2', rotation=90, ha='center', va='center', 
      transform=cbar.ax.transAxes, color='gray') 

# Make the grouping clearer 
ax.set_xticks(np.arange(0, 2 * ncols, 2), minor=True) 
ax.grid(axis='x', ls='-', color='gray', which='minor') 
ax.grid(axis='y', ls=':', color='gray') 

plt.show() 

enter image description here

+0

这个效果非常好,谢谢 – Charlietrypsin