简体   繁体   English

Seaborn 基于行数的热图大小

[英]Seaborn Heatmap size based on number of rows

I'm writing a code that plots 4 different heatmaps.我正在编写一个绘制 4 个不同热图的代码。 These heatmaps represents data for each week of the year over 4 years: rows are weeks (first week, second week and so on), columns are my data for the week and each heatmap represents a different year.这些热图代表 4 年来一年中每周的数据:行是周(第一周、第二周等),列是我这周的数据,每个热图代表不同的年份。

This is how it looks so far:到目前为止,它是这样的: 热图

As you might notice, the first 3 years have 52 weeks (the whole year) while for the last one (which is 2020) I have data only for the first 30 weeks.您可能会注意到,前 3 年有 52 周(全年),而最后一年(2020 年)我只有前 30 周的数据。

Since I'm plotting these heatmaps to compare them one against each other, I would like to have the last heatmap "cut": instead of having the same exact size of all the others, i would like to align the rows with the other heatmaps (so basically, if compared with the other heatmaps, it will be overall almost half the size of the other, having only 30 rows).由于我正在绘制这些热图以将它们相互比较,所以我希望最后一个热图“剪切”:我不想让所有其他热图具有相同的精确大小,而是希望将行与其他热图对齐(所以基本上,如果与其他热图相比,它的整体大小几乎是另一个热图的一半,只有 30 行)。

My code so far is the following:到目前为止,我的代码如下:

#------getting the data --------
array_2017 = np.array(list_data_years[0]) #[52,200] matrix
#array_2017 = (array_2017 - np.mean(array_2017)) / np.std(array_2017)

array_2018 = np.array(list_data_years[1]) #[52,200] matrix
#array_2018 = (array_2018 - np.mean(array_2018)) / np.std(array_2018)

array_2019 = np.array(list_data_years[2]) #[52,200] matrix
#array_2019 = (array_2019 - np.mean(array_2019)) / np.std(array_2019)

array_2020 = np.array(list_data_years[3]) #[30,200] matrix
#array_2020 = (array_2020 - np.mean(array_2020)) / np.std(array_2020)
----------

#--------plotting--------
fig = plt.figure(figsize=(15,15))
gs0 = gridspec.GridSpec(2,2, figure=fig, hspace=0.2)

ax0 = fig.add_subplot(gs0[0,0])
ax1 = fig.add_subplot(gs0[0,1])
ax2 = fig.add_subplot(gs0[1,0])
ax3 = fig.add_subplot(gs0[1,1])

sns.heatmap(array_2017, ax=ax0, vmin =0, vmax = 2000000)
sns.heatmap(array_2018, ax=ax1, vmin =0, vmax = 2000000)
sns.heatmap(array_2019, ax=ax2, vmin =0, vmax = 2000000)
sns.heatmap(array_2020, ax=ax3,vmin =0, vmax = 2000000)

One easy solution is to pad your array_2020 with NaN so that it has a (52,200) shape一个简单的解决方案是用 NaN 填充array_2020 ,使其具有 (52,200) 形状

array_2020 = np.pad(array_2020, ((0,52-array_2020.shape[0]),(0,0)), constant_values=np.NaN)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM