2016-03-01 116 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