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

时间:2021-01-05 23:39:33

I currently have two large data sets, and I want to compare them. I have them separately, one in red and one in blue, however I would like to show the red and blue side by side. How might I go about this?

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

My current code is:

我目前的代码是:

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

Both data and data1 are formed of 140 different lists with information extracted from 280 different files, is there a way I can still use these two lists in order to create a heatmap which will show these data in the same figure?

数据和数据1都是由140个不同的列表组成,信息是从280个不同的文件中提取的,有没有一种方法我仍然可以使用这两个列表来创建一个热图,它将在同一图中显示这些数据?

So for example my heatmap will be /red/blue/red/blue etc

例如,我的热图将是/红色/蓝色/红色/蓝色等

Here is an example of my heatmap:

以下是我的热图的示例:

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

EDIT:

While not showing exactly what I want, I have made a heatmap of the difference in values between the two previous heatmaps.

虽然没有准确显示我想要的内容,但我已经制作了前两个热图之间值差异的热图。

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

例如: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()

1 个解决方案

#1


2  

As @jeanrjc mentioned, this is conceptually very similar to a a previously-asked question. However, it's probably not obvious how to apply that method in your case.

正如@jeanrjc所提到的,这在概念上非常类似于之前提出的问题。但是,在您的情况下如何应用该方法可能并不明显。

Here's a minimal example of plotting two arrays with the same shape "side-by-side" with two different colormaps. The key is to independently plot two masked arrays. To create these masked arrays, we'll make new arrays with double the number of columns and mask every other column.

这是一个最小的例子,用两个不同的颜色图绘制两个具有“并排”相同形状的阵列。关键是要独立绘制两个蒙版数组。要创建这些蒙版数组,我们将使用双列数来创建新数组,并屏蔽每隔一列。

Here's a simple example (note that there are several ways to create the masked array pattern):

这是一个简单的例子(请注意,有几种方法可以创建蒙版数组模式):

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

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

And if we wanted to make a fancier version, we could do something similar to:

如果我们想制作一个更好的版本,我们可以做类似的事情:

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

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

#1


2  

As @jeanrjc mentioned, this is conceptually very similar to a a previously-asked question. However, it's probably not obvious how to apply that method in your case.

正如@jeanrjc所提到的,这在概念上非常类似于之前提出的问题。但是,在您的情况下如何应用该方法可能并不明显。

Here's a minimal example of plotting two arrays with the same shape "side-by-side" with two different colormaps. The key is to independently plot two masked arrays. To create these masked arrays, we'll make new arrays with double the number of columns and mask every other column.

这是一个最小的例子,用两个不同的颜色图绘制两个具有“并排”相同形状的阵列。关键是要独立绘制两个蒙版数组。要创建这些蒙版数组,我们将使用双列数来创建新数组,并屏蔽每隔一列。

Here's a simple example (note that there are several ways to create the masked array pattern):

这是一个简单的例子(请注意,有几种方法可以创建蒙版数组模式):

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

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

And if we wanted to make a fancier version, we could do something similar to:

如果我们想制作一个更好的版本,我们可以做类似的事情:

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

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