在seaborn中合并两个热图

时间:2018-09-13 11:02:40

标签: python matplotlib seaborn heatmap

我面临着seaborn和matplotlib中两个热图的问题。

我的问题是,我需要组合两个热图以显示主要差异。我有一些数据点(x,y),有些是红色的热图,有些是蓝色的。目标是这些的组合,看起来应该像这样。

目标热图:
Goal

现在我有两张地图:

地图1: enter image description here

地图2: enter image description here

我的代码如下所示。

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

import pandas as pd
data=pd.read_csv('data.csv')
data.head(10)

red = data.loc[data.type == "red"]
blue = data.loc[data.type == "blue"]

ax = sns.kdeplot(blue.x_pos, blue.y_pos,
                  cmap="Blues", n_levels=15, shade=True, shade_lowest=False, legend=False)

ax = sns.kdeplot(red.x_pos, red.y_pos,
                  cmap="Reds", n_levels=15, shade=True, shade_lowest=False, legend=False)

plt.ylim(-300, 300)
plt.xlim(-300, 300)


ax.set_frame_on(False)
ax.set_yticklabels([])
ax.set_xticklabels([])

plt.gca().axes.get_yaxis().set_visible(False)
plt.gca().axes.get_xaxis().set_visible(False)

plt.show()

CSV文件

x_pos,y_pos,type
108,-204,red
108,-168,red
108,-228,red
108,-192,red
108,-144,red
108,108,red
108,180,red
108,192,red
108,228,red
108,-240,red
108,-216,red
108,-180,red
108,-156,blue
108,-132,red
108,-120,red
108,-108,blue
108,-96,red
108,-84,red
108,-72,red
108,-60,blue
108,-48,red
108,-24,blue
108,0,red
108,72,red
108,120,red
108,132,red
108,144,red
108,156,blue
108,204,red
108,240,red
108,-12,red
108,168,blue
108,216,blue
108,252,blue
96,-156,red
96,-36,red
96,-192,red
96,-72,red
96,-48,red
96,144,red
96,204,red
96,-240,red
96,-228,red
96,-204,red
96,-168,red
96,-144,blue
96,-120,blue
96,-96,blue
96,-12,red
96,12,blue
96,36,red
96,72,red
96,96,red
96,108,red
96,168,blue
96,192,red
96,228,blue
96,252,blue
96,-108,red
96,-60,red
96,48,red
96,156,blue
96,180,blue
96,-252,red
96,-180,red
96,-132,blue
96,120,red
-108,-168,red
-108,108,red
-108,-240,blue
-108,-228,blue
-108,-216,blue
-108,-204,blue
-108,-156,red
-108,-144,red
-108,-120,red
-108,-108,blue
-108,-96,red
-108,-84,red
-108,-60,red
-108,0,blue
-108,36,blue
-108,48,red
-108,60,red
-108,120,red
-108,144,red
-108,180,red
-108,192,blue
-108,204,blue
-108,240,red
-108,-192,blue
-108,-180,blue
-108,-36,blue
-108,-12,red
-108,24,blue
-108,156,red
-108,168,red
-108,252,red
-108,-132,red
-108,-24,red
-108,72,red
-108,84,blue
-108,264,red
-108,96,red
-108,228,red
-96,-156,red
-96,180,red
-96,-240,blue
-96,-204,red
-96,-192,blue
-96,-180,blue
-96,-168,red
-96,-96,red
-96,-48,red
-96,-24,red
-96,0,red
-96,72,blue
-96,84,blue
-96,96,blue
-96,132,red
-96,192,blue
-96,228,blue
-96,-252,blue
-96,-216,red
-96,-144,red
-96,48,red
-96,108,red
-96,-132,red
-96,-60,blue
-96,36,blue
-96,-84,blue
132,168,red
132,-216,red
132,-192,red
132,-168,blue
132,-156,blue
132,-120,red
132,-84,red
132,-48,blue
132,-36,red
132,0,red
132,24,red
132,84,red
132,120,red
132,144,red
132,192,red
132,216,red
132,240,red
132,-240,red
132,-228,red
132,-180,red
132,48,red
132,156,blue
132,228,red
132,96,red
132,108,red
132,-132,red
132,-24,red
132,60,red
228,36,red
228,48,blue
228,-72,blue
228,-60,red
228,-36,blue
228,-12,blue
228,0,blue
228,24,blue
228,72,blue
228,84,red
228,96,red
228,192,red
228,204,red
228,240,red
228,-108,red
228,-84,blue
228,-192,red
228,-156,red
228,60,blue
228,180,red
228,156,red
228,12,blue
144,72,red
144,-240,red
144,-192,red
144,-180,red
144,-156,blue
144,0,blue
144,24,red
144,36,red
144,48,red
144,84,blue
144,108,red
144,216,red
144,228,red
144,-276,red
144,-228,red
144,-144,red
144,-132,blue
144,-48,red
144,120,blue
144,156,red
144,192,red
144,-72,red
144,-12,red
144,-84,red
144,60,blue
216,-228,red
216,12,red
216,-240,red
216,-168,red
216,-108,blue
216,-96,blue
216,-48,blue
216,-36,blue
216,0,blue
216,60,blue
216,84,red
216,108,red
216,180,red
216,-216,red
216,48,blue
216,96,blue
216,168,red
216,-72,blue
216,-12,red
216,-156,red
216,228,blue
156,-156,red
156,60,red
156,-144,red
156,-84,red
156,-48,red
156,-36,red
156,-24,red
156,84,red
156,180,red
156,192,red
156,204,blue
156,228,red
156,-252,red
156,-240,red
156,-108,blue
156,0,blue
156,240,red
156,-216,red
156,-192,blue
156,-60,red
156,48,blue
156,132,red
156,252,red
156,276,red
-216,0,red
-216,84,red
-216,-276,red
-216,-144,blue
-216,-84,blue
-216,-36,red
-216,-12,red
-216,12,red
-216,24,blue
-216,60,red
-216,108,red
-216,120,red
-216,-156,red
-216,-48,blue
-216,36,blue
-216,72,red
-216,96,red
-216,-240,blue
-216,-72,blue
-216,132,red
-216,264,red
-192,-216,red
-192,-144,red
-192,-120,red
-192,-108,red
-192,-36,red
-192,-12,red
-192,24,red
-192,36,red
-192,72,blue
-192,96,red
-192,216,red
-192,-48,blue
-192,12,red
-192,48,red
-192,84,red
-192,108,red
-192,276,red
-192,-252,red
-192,-192,red
-192,132,red
-192,156,blue
-192,-180,red
-192,264,red
-156,-216,red
-156,-156,red
-156,-132,red
-156,-120,blue
-156,-84,red
-156,-72,red
-156,-12,red
-156,0,red
-156,12,red
-156,36,blue
-156,48,red
-156,132,blue
-156,156,red
-156,180,red
-156,192,blue
-156,-192,red
-156,-168,red
-156,-108,blue
-156,108,red
-156,144,red
-156,-276,red
-156,-96,red
-156,-24,red
-156,120,blue
-156,-36,blue
-156,216,red
-156,228,red
-156,264,red
-144,204,blue
-144,-192,blue
-144,-156,blue
-144,-120,red
-144,-108,blue
-144,-72,red
-144,-60,red
-144,24,red
-144,48,red
-144,96,red
-144,156,red
-144,168,red
-144,-276,red
-144,-36,red
-144,-24,red
-144,0,red
-144,72,red
-144,216,blue
-144,-84,blue
-144,108,blue
-144,-252,red
-144,-180,blue
192,-12,blue
192,-228,red
192,-144,red
192,-48,red
192,-24,blue
192,36,red
192,84,blue
192,144,red
192,156,red
192,180,red
192,228,blue
192,-108,blue
192,24,blue
192,216,red
192,240,red
192,-240,red
192,-204,red
192,-60,blue
-180,-228,blue
-180,-132,red
-180,-120,red
-180,-96,red
-180,-72,red
-180,-48,blue
-180,-36,red
-180,-12,red
-180,96,red
-180,156,blue
-180,-240,red
-180,-144,blue
-180,-24,blue
-180,36,red
-180,48,red
-180,132,red
-180,-168,red
-180,-84,blue
-180,12,blue
-180,168,red
-180,204,red
-180,-264,red
-180,24,blue
-120,108,red
-120,-240,blue
-120,-192,red
-120,-156,red
-120,-132,red
-120,-120,red
-120,-108,red
-120,-96,red
-120,48,red
-120,96,red
-120,144,red
-120,168,red
-120,192,red
-120,-84,red
-120,0,red
-120,24,red
-120,120,red
-120,156,red
-120,180,red
-120,216,red
-120,252,red
-120,-36,red
-120,36,red
-120,60,blue
-120,132,red
-120,240,blue
120,-240,red
120,-216,red
120,-192,red
120,-180,red
120,-84,blue
120,-36,red
120,108,red
120,192,blue
120,204,red
120,228,red
120,-264,blue
120,-204,red
120,36,red
120,72,red
120,96,blue
120,0,red
120,-252,red
120,-168,red
120,-156,blue
120,-120,red
120,-60,red
120,24,blue
120,216,blue
120,-108,red
120,144,blue
-204,204,red
-204,-264,red
-204,-216,red
-204,-96,blue
-204,24,red
-204,72,red
-204,96,red
-204,144,red
-204,168,red
-204,240,red
-204,-168,red
-204,-132,red
-204,-60,blue
-204,-48,red
-204,-36,blue
-204,0,red
-204,48,red
-204,108,red
-204,156,red
-204,-84,blue
-204,-24,blue
-204,60,blue
-204,-252,blue
-204,12,red
-132,-132,red
-132,-216,red
-132,-144,red
-132,-60,red
-132,-24,red
-132,-12,red
-132,0,blue
-132,24,red
-132,36,red
-132,48,red
-132,72,blue
-132,84,red
-132,168,red
-132,-84,red
-132,-36,blue
-132,96,red
-132,132,red
-132,-204,red
-132,-96,red
-132,-48,blue
-132,156,blue
-132,192,blue
-132,-192,red
-132,-108,blue
-132,-72,blue
180,-252,red
180,-204,red
180,-84,blue
180,-48,red
180,0,blue
180,144,red
180,180,red
180,192,blue
180,240,red
180,228,red
180,-264,red
180,-132,blue
180,36,blue
180,-36,red
180,84,blue
180,216,red
180,-228,red
180,168,red
204,60,red
204,-120,red
204,-108,red
204,-48,red
204,-36,blue
204,-24,red
204,0,blue
204,12,blue
204,108,red
204,156,red
204,180,red
204,228,red
204,-228,red
204,-192,red
204,-84,blue
204,48,blue
204,-240,red
204,-180,red
204,-96,red
204,240,blue
204,252,red
204,-276,red
204,-72,blue
240,60,blue
240,-36,blue
240,-24,blue
240,24,blue
240,36,blue
240,48,red
240,144,red
240,156,red
240,168,red
240,-72,red
240,72,red
240,-132,red
240,108,red
240,84,blue
240,-120,red
-228,-216,red
-228,-108,red
-228,-84,red
-228,-36,blue
-228,-24,red
-228,-12,red
-228,12,blue
-228,24,blue
-228,72,red
-228,48,red
-228,-48,blue
-228,36,blue
-228,84,blue
-228,108,red
-228,-180,red
-168,12,red
-168,-132,red
-168,-120,blue
-168,-84,blue
-168,0,red
-168,60,red
-168,84,blue
-168,216,red
-168,276,red
-168,24,red
-168,-240,blue
-168,-228,blue
-168,36,red
-168,96,red
-168,120,blue
-168,168,red
-168,-144,red
-168,-12,red
-168,48,red
-168,-252,red
-168,-72,blue
-168,156,blue
-240,24,red
-240,-132,blue
-240,-24,red
-240,0,red
-240,36,blue
-240,60,blue
-240,72,blue
-240,228,red
-240,-60,red
-240,48,blue
-240,-96,red
-240,-48,blue
168,-252,red
168,-228,red
168,-216,red
168,-72,red
168,48,red
168,84,red
168,120,red
168,168,red
168,180,red
168,240,red
168,228,red
168,-132,red
168,144,red
168,192,red
168,-108,blue
168,108,blue
168,156,blue
168,0,blue
168,132,red
-84,-120,red
-84,-96,red
-84,-60,red
-84,-24,red
-84,0,red
-84,84,red
-84,132,red
-84,144,red
-84,228,red
-84,252,red
-84,-192,red
-84,96,red
-84,108,red
-84,-276,red
-84,-204,red
-84,60,red
-84,192,red
84,240,red
84,-216,red
84,-108,blue
84,-72,red
84,-48,red
84,-36,blue
12,-168,red
12,-132,red
12,168,red
12,-216,red
12,-12,red
12,0,red
12,204,red
12,72,red
-252,0,red
-252,24,red
-252,36,red
-252,60,red
-24,-228,red
-24,-216,red
-24,96,red
-24,180,red
-24,-240,red
-24,-132,red
-24,-12,red
-24,72,red
-24,108,red
-24,144,red
-12,-108,red
-12,132,red
-12,228,red
-12,-156,red
-12,156,red
-12,216,red
36,-264,red
36,-228,red
36,-108,red
36,-12,red
36,156,red
36,180,red
36,-144,red
36,-36,red
36,96,red
-72,-204,red
-72,-24,red
-72,0,red
-72,-240,red
-72,-96,red
-72,24,red
-72,216,red
-36,-156,red
-36,-72,red
-36,168,red
-36,144,red
-36,156,red
48,-60,red
48,144,red
48,216,red
48,-36,red
48,168,red
48,-216,red
60,-84,red
60,264,red
60,-276,red
60,168,red
-60,-144,red
-60,-48,red
-60,240,red
-60,-96,red
-48,-228,red
-48,180,red
-48,-60,red
-48,192,red
0,84,red
0,204,red
0,-36,red
0,-240,red
0,192,red
0,-12,red
0,120,red
0,264,red
24,-180,red
24,-252,red
24,-240,red
24,-24,red
252,96,red
252,216,red
252,180,red
72,-252,red
108,-36,blue
108,12,blue
108,36,blue
108,24,blue
108,84,blue
108,276,blue
108,96,blue
108,-264,blue
-96,204,blue
-96,-36,blue
-96,60,blue
-96,-264,blue
-96,-120,blue
-96,240,blue
-96,252,blue
96,-84,blue
96,60,blue
96,264,blue
96,-264,blue
96,-216,blue
-108,-252,blue
228,-24,blue
228,120,blue
228,132,blue
228,-120,blue
228,-264,blue
228,-180,blue
228,-132,blue
228,252,blue
228,-96,blue
240,-192,blue
240,-180,blue
240,-108,blue
240,-12,blue
240,0,blue
240,12,blue
240,132,blue
240,192,blue
240,228,blue
240,-168,blue
132,-60,blue
132,12,blue
132,132,blue
132,-108,blue
132,264,blue
132,-276,blue
156,-72,blue
156,-168,blue
156,-120,blue
156,36,blue
156,108,blue
156,144,blue
156,168,blue
156,216,blue
156,-204,blue
156,-180,blue
156,-132,blue
156,-12,blue
156,120,blue
156,264,blue
192,-84,blue
192,-216,blue
192,-192,blue
192,-132,blue
192,-120,blue
192,48,blue
192,60,blue
192,96,blue
192,-264,blue
192,-252,blue
192,108,blue
192,132,blue
192,-96,blue
192,192,blue
216,-120,blue
216,-84,blue
216,-60,blue
216,144,blue
216,216,blue
216,120,blue
216,192,blue
216,204,blue
216,240,blue
216,24,blue
-240,-204,blue
-240,-120,blue
-240,-84,blue
-240,-216,blue
-240,96,blue
-240,144,blue
-240,-192,blue
180,-24,blue
180,-72,blue
180,-12,blue
180,24,blue
180,96,blue
180,156,blue
180,132,blue
180,-180,blue
180,48,blue
180,-144,blue
180,276,blue
-216,-216,blue
-216,-60,blue
-216,192,blue
-216,-192,blue
-216,-168,blue
-216,216,blue
-216,252,blue
120,-72,blue
120,-12,blue
120,132,blue
120,156,blue
120,-228,blue
120,180,blue
120,120,blue
120,-96,blue
144,-168,blue
144,-36,blue
144,12,blue
144,180,blue
144,-108,blue
144,264,blue
144,-252,blue
144,-120,blue
144,-96,blue
144,144,blue
144,252,blue
-144,-48,blue
-144,84,blue
-144,240,blue
-144,-240,blue
-144,-144,blue
-144,-96,blue
-144,36,blue
-144,144,blue
-144,60,blue
-204,-156,blue
-204,-120,blue
-204,-108,blue
-204,-72,blue
-204,132,blue
-204,-240,blue
-204,-228,blue
-204,-144,blue
-204,228,blue
-204,264,blue
-204,192,blue
-132,-180,blue
-132,60,blue
-132,120,blue
-132,228,blue
-132,-120,blue
-132,204,blue
-132,216,blue
-132,-240,blue
-132,-228,blue
-192,-156,blue
-192,-228,blue
-192,-168,blue
-192,192,blue
-192,-84,blue
-192,-240,blue
-192,228,blue
-168,-192,blue
-168,-180,blue
-168,-36,blue
-168,72,blue
-168,228,blue
-168,-96,blue
-168,-60,blue
-168,108,blue
-168,144,blue
-168,-156,blue
-168,132,blue
-156,-240,blue
-156,-228,blue
-156,-180,blue
-156,-60,blue
-156,204,blue
-156,-204,blue
-156,168,blue
-156,-48,blue
-156,84,blue
-156,60,blue
168,252,blue
168,-276,blue
168,-60,blue
168,60,blue
168,264,blue
168,-144,blue
168,-120,blue
168,-12,blue
168,-264,blue
168,-84,blue
-228,-120,blue
-228,-60,blue
-228,144,blue
-228,-96,blue
-228,96,blue
-228,120,blue
-228,156,blue
-228,-156,blue
-228,60,blue
-228,216,blue
-180,84,blue
-180,192,blue
-180,-204,blue
-180,60,blue
-180,-276,blue
-180,-156,blue
-180,264,blue
204,-204,blue
204,-156,blue
204,-132,blue
204,84,blue
204,120,blue
204,132,blue
204,144,blue
204,-216,blue
204,24,blue
204,-264,blue
204,96,blue
-120,-180,blue
-120,204,blue
-120,228,blue
-120,-12,blue
-120,72,blue
-120,-204,blue
-120,-48,blue
84,-132,blue
84,-12,blue
84,24,blue
84,84,blue
84,156,blue
84,180,blue
84,192,blue
84,-228,blue
84,-156,blue
84,48,blue
84,-264,blue
-84,216,blue
-252,72,blue
-252,108,blue
-252,-60,blue
12,216,blue
12,-264,blue
12,-228,blue
12,60,blue
12,108,blue
-60,-192,blue
-60,-252,blue
-60,-204,blue
-60,-156,blue
-60,-36,blue
36,0,blue
36,48,blue
36,120,blue
36,144,blue
36,-204,blue
36,84,blue
36,-72,blue
-12,-72,blue
-12,120,blue
-12,-84,blue
-48,-216,blue
-48,168,blue
-48,-168,blue
-48,228,blue
-48,144,blue
-48,0,blue
-36,108,blue
-36,-204,blue
-36,-132,blue
-24,-204,blue
-24,60,blue
-24,240,blue
0,180,blue
0,-120,blue
24,48,blue
24,-228,blue
24,-204,blue
48,36,blue
48,-228,blue
48,-24,blue
48,72,blue
60,192,blue
60,0,blue
60,12,blue
60,228,blue
252,-84,blue
252,-72,blue
252,36,blue
252,-60,blue
252,-48,blue
72,228,blue
72,84,blue
72,156,blue
72,12,blue

是否存在某种方法可以在Seaborn中做到? 谢谢

1 个答案:

答案 0 :(得分:0)

好吧,这是有效的,它是从here复制/改编的。

将数据放入“ data.csv”

import numpy as np
import pandas as pd
# import seaborn as sns # No longer used
import matplotlib.pyplot as plt
from scipy import stats

df = pd.read_csv('data.csv')
red_df = df[df['type']=='red']
blue_df = df[df['type']=='blue']

def compute_KDE_from_pos(x_pos,y_pos):

    xmin = x_pos.min()
    xmax = x_pos.max()
    ymin = y_pos.min()
    ymax = y_pos.max()

    X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
    positions = np.vstack([X.ravel(), Y.ravel()])
    values = np.vstack([x_pos, y_pos])
    kernel = stats.gaussian_kde(values)
    Z = np.reshape(kernel(positions).T, X.shape)

    return Z

Z_red = compute_KDE_from_pos(red_df.x_pos,red_df.y_pos)
Z_blue = compute_KDE_from_pos(blue_df.x_pos,blue_df.y_pos)
Z_diff = Z_red-Z_blue

fig, ax = plt.subplots()
ax.imshow(np.rot90(Z_diff), cmap='RdBu',
           extent=[df.x_pos.min(), df.x_pos.max(), df.y_pos.min(), df.y_pos.max()])
ax.plot(red_df.x_pos, red_df.y_pos, 'r.', markersize=2)
ax.plot(blue_df.x_pos, blue_df.y_pos, 'b.', markersize=2)
ax.set_xlim([df.x_pos.min(), df.x_pos.max()])
ax.set_ylim([df.y_pos.min(), df.y_pos.max()])
plt.show()

输出:

enter image description here

编辑:

我写了更干净的代码版本。您可能仍要检查使用的颜色图是否符合您的期望,或者是否必须将红色与蓝色反转。无论哪种方式,此代码都可以回答问题。