gpt4 book ai didi

python - 使用两个不同颜色的数据集创建 matplotlib 热图

转载 作者:行者123 更新时间:2023-11-28 16:27:05 46 4
gpt4 key购买 nike

我目前有两个大数据集,我想比较它们。我分别有它们,一个是红色的,一个是蓝色的,但是我想并排显示红色和蓝色。我该怎么做?

我当前的代码是:

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 都由 140 个不同的列表组成,信息从 280 个不同的文件中提取,有没有办法我仍然可以使用这两个列表来创建将在同一图中显示这些数据的热图?

例如,我的热图将是/red/blue/red/blue 等

这是我的热图示例:

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()

最佳答案

正如@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

关于python - 使用两个不同颜色的数据集创建 matplotlib 热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35727374/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com