如何创建具有多个颜色图的热图? [英] How to create a heat-map with multiple colormaps?

查看:57
本文介绍了如何创建具有多个颜色图的热图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下图所示,我正在寻找一种简单的方法将两个或多个热图合并为一个,即具有多个颜色图的热图.

As illustrated below, I am looking for an easy way to combine two or more heat-maps into one, i.e., a heat-map with multiple colormaps.

这个想法是将每个单元格分成多个子单元格.我找不到任何已经实现了这种可视化功能的python库.有人知道(至少)接近于此的东西吗?

The idea is to break each cell into multiple sub-cells. I couldn't find any python library with such a visualization function already implemented. Anybody knows something (at least) close to this?

推荐答案

可以逐列绘制热图.白色网格线可以标记单元格边框.

The heatmaps can be drawn column by column. White gridlines can mark the cell borders.

import numpy as np
from matplotlib import pyplot as plt

a = np.random.random((5, 6))
b = np.random.random((5, 6))
vmina = a.min()
vminb = b.min()
vmaxa = a.max()
vmaxb = b.max()

fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(10,3), gridspec_kw={'width_ratios':[1,1,2]})

ax1.imshow(a, cmap='Reds', interpolation='nearest', origin='lower', vmin=vmina, vmax=vmaxa)
ax1.set_xticks(np.arange(.5, a.shape[1]-1, 1), minor=True)
ax1.set_yticks(np.arange(.5, a.shape[0]-1, 1), minor=True)

ax2.imshow(b, cmap='Blues', interpolation='nearest', origin='lower', vmin=vminb, vmax=vmaxb)
ax2.set_xticks(np.arange(.5, a.shape[1]-1, 1), minor=True)
ax2.set_yticks(np.arange(.5, a.shape[0]-1, 1), minor=True)

for i in range(a.shape[1]):
    ax3.imshow(a[:,i:i+1], extent=[2*i-0.5, 2*i+0.5, -0.5, a.shape[0]-0.5 ],
               cmap='Reds', interpolation='nearest', origin='lower', vmin=vmina, vmax=vmaxa)
    ax3.imshow(b[:,i:i+1], extent=[2*i+0.5, 2*i+1.5, -0.5, a.shape[0]-0.5 ],
               cmap='Blues', interpolation='nearest', origin='lower', vmin=vminb, vmax=vmaxb)
ax3.set_xlim(-0.5, 2*a.shape[1] -0.5 )
ax3.set_xticks(np.arange(1.5, 2*a.shape[1]-1, 2), minor=True)
ax3.set_yticks(np.arange(.5, a.shape[0]-1, 1), minor=True)

for ax in (ax1, ax2, ax3):
    ax.grid(color='white', which='minor', lw=2)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.tick_params(axis='both', which='both', size=0)

plt.show()

PS:如果简洁是一个重要因素,则可以省略所有装饰、细节和比较:

PS: If brevity were an important factor, all embellishments, details and comparisons could be left out:

# import numpy as np
# from matplotlib import pyplot as plt

a = np.random.random((5, 6))
b = np.random.random((5, 6))
norma = plt.Normalize(vmin=a.min(), vmax=a.max())
normb = plt.Normalize(vmin=b.min(), vmax=b.max())

for i in range(a.shape[1]):
    plt.imshow(a[:, i:i + 1], extent=[2*i-0.5, 2*i+0.5, -0.5, a.shape[0]-0.5], cmap='Reds', norm=norma)
    plt.imshow(b[:, i:i + 1], extent=[2*i+0.5, 2*i+1.5, -0.5, a.shape[0]-0.5], cmap='Blues', norm=normb)
plt.xlim(-0.5, 2*a.shape[1]-0.5)
# plt.show()

这篇关于如何创建具有多个颜色图的热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆