结合Seaborn中的两个热图 [英] Combining two heat maps in seaborn

查看:71
本文介绍了结合Seaborn中的两个热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据表,尺寸为 4x25 .每个表都来自不同的时间点,但是具有完全相同的元数据,实质​​上是相同的列和行标题.

I have 2 data tables with the dimensions 4x25. Each table is from a different point in time, but has exactly the same meta data, in essence the same column and row headers.

鉴于有大量的列,我认为最好使用 seaborn 库(用于 Python )使用 heatmap 来表示.但是,我需要将两个表都包含在同一图中.这样我就可以创建表示单个数据表的单个热图.

Given the large number of columns, I thought it best to represent this using a heatmap using the seaborn library for Python. However, I need to include both tables in the same plot. I am able to create a single heatmap representing a single data table as so.

df = pd.DataFrame(raw_data)
ax = sns.heatmap(df)
ax.set(yticklabels=labels)

但是,我不确定如何将两个数据表合并到同一张热图中.我能想到的唯一方法是只创建一个尺寸为 4x50 的新 DataFrame ,然后将两个表都放入该表中,并使用热图进行绘制.但是,在以下问题上,我需要帮助:

However, I'm not sure how to combine two data tables into the same heatmap. The only way I can think of is to just create a new DataFrame of dimension 4x50 and then fit both tables into that one and plot that using the heatmap. But then, I need help with the following issues:

  1. 我不确定如何在热图的中间绘制一条线以区分2个表中的数据.读者看到列从何处开始重复以了解新数据从何处开始,会很烦人.
  2. 一个更好的解决方案是对同一热图内的两套数据应用2种不同的着色方案,而不仅仅是在中间绘制一条线.

任何有关上述问题的帮助都将非常有帮助.

Any help with the above issues would be very helpful.

注意:我不打算像上面建议的那样使用数据,甚至不使用热图.如果还有其他建议,请告诉我.

Note: I'm not bent on representing the data as I've suggested above or even using a heatmap. If there are other suggestions for plotting, please let me know.

推荐答案

在图形中并排显示两个海洋热图的一种可能方法是将它们绘制到各个子图上.可以将子图之间的间距设置得很小( wspace = 0.01 ),然后将相应的颜色条和刻度标签放置在该间隙之外.

One possible way of showing two seaborn heatmaps side by side in a figure would be to plot them to individual subplots. One may set the space between the subplots to very small (wspace=0.01) and position the respective colorbars and ticklabels outside of that gap.

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

df =  pd.DataFrame(np.random.rand(25,4), columns=list("ABCD"))
df2 = pd.DataFrame(np.random.rand(25,4), columns=list("WXYZ"))

fig, (ax,ax2) = plt.subplots(ncols=2)
fig.subplots_adjust(wspace=0.01)
sns.heatmap(df, cmap="rocket", ax=ax, cbar=False)
fig.colorbar(ax.collections[0], ax=ax,location="left", use_gridspec=False, pad=0.2)
sns.heatmap(df2, cmap="icefire", ax=ax2, cbar=False)
fig.colorbar(ax2.collections[0], ax=ax2,location="right", use_gridspec=False, pad=0.2)
ax2.yaxis.tick_right()
ax2.tick_params(rotation=0)
plt.show()

这篇关于结合Seaborn中的两个热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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