带有分隔列的 matplotlib 热图

matplotlib heatmap with separated columns

我有一个多列的热图,看起来像这样:

这几乎正是我想要的。我是用imshow做的,绘图代码很简单,数据是二维numpy数组:

plt.imshow(data, cmap="hot", 
               vmin=0.0, vmax=1.0, aspect='auto',  
               interpolation='nearest')
plt.colorbar()
plt.show()

理想情况下,此头图中的列应该分组(或分解),因为它们代表相关的事物。有没有什么简单的方法可以创建此图的一个版本,比如说,我可以将 0-4、5-10 和 11-22 列分成单独的块,而无需重复颜色栏或 yaxis 标签,但可能带有唯一标签对于每组列?

理想情况下,我想要一个看起来像(ascii 艺术)的情节:

0   +---+   +-------+  +-------------+   +-+ 1.0
    |   |   |       |  |             |   | | 
500 |   |   |       |  |             |   | |
    |   |   |       |  |             |   | |
1000+---+   +-------+  +-------------+   +-+ 0.0
     L1      Label2       Label3           

有什么想法吗?

您可以轻松地分割您的 data 数组 using standard numpy array notation

之后,只需创建具有正确几何形状的轴即可。 You could use Gridspec for that, or the sightly simpler plt.subplots()版本。

data = np.random.random(size=(1000,22))

fig, axs = plt.subplots(1,3,sharey=True,gridspec_kw={'width_ratios':[5,6,12]})
a1 = axs[0].imshow(data[:,:5], cmap="hot", 
               vmin=0.0, vmax=1.0, aspect='auto',  
               interpolation='nearest')
a2 = axs[1].imshow(data[:,5:10], cmap="hot", 
               vmin=0.0, vmax=1.0, aspect='auto',  
               interpolation='nearest')
a3 = axs[2].imshow(data[:,11:], cmap="hot", 
               vmin=0.0, vmax=1.0, aspect='auto',  
               interpolation='nearest')

for ax,l in zip(axs,['Label 1','Label 2','Label 3']):
    ax.set_xticklabels([])
    ax.set_xlabel(l)

plt.colorbar(a1)

plt.show()