简体   繁体   English

如何在 python 中为 2 个不同比例的 2 列创建热图?

[英]How to create a heatmap for 2 columns at 2 different scales in python?

Im trying to create a heatmap which allows me to represent two different columns from the same data frame.我试图创建一个热图,它允许我代表同一数据框中的两个不同列。 I was hoping to have 2 colour-bars on the right of the heatmap to represent the scales from each column.我希望在热图的右侧有 2 个颜色条来表示每列的比例。 Here is how part of my dataframe looks (the full dataframe contains 244 rows):这是我的 dataframe 的一部分的外观(完整的 dataframe 包含 244 行):

Country      Count     Score
------------------------------
America      12455     1.23
Mexico       245667    16.22
China        12221     5.445
Belgium      345632    8.23
Turkey       12342     11.4
India        45643     4.2
China        123556    17.8

I was hoping that along the y-axis I could list the country names, then have two x-axis maps for both the count and score and different scales - then on the side have a colour bar for both the Count and Score separately.我希望沿着 y 轴我可以列出国家名称,然后有两个 x 轴地图用于计数和分数以及不同的比例 - 然后在侧面分别有一个颜色条用于计数和分数。 Is it possible to create something like this using seaborn?是否可以使用 seaborn 创建类似的东西?

I have attached an example of how I'm hoping the Heatmap could look like:我附上了一个我希望热图看起来像这样的例子:

在此处输入图像描述

Thanks谢谢

You can create your heatmap "by hand" fairly easily using imshow() .您可以使用imshow()相当轻松地“手动”创建热图。 It is just a matter of placing the resulting image correctly in the axes using the extent= keyword.只需使用extent=关键字将生成的图像正确放置在轴中即可。

fig, ax = plt.subplots()
N = df.index.size

# first heatmap
im1 = ax.imshow(np.vstack([df['Count'],df['Count']]).T, aspect='auto', extent=[-0.5,0.5,-0.5,N-0.5], origin='lower', cmap='magma')
# second heatmap
im2 = ax.imshow(np.vstack([df['Score'],df['Score']]).T, aspect='auto', extent=[0.5,1.5,-0.5,N-0.5], origin='lower', cmap='Blues')

cbar1 = fig.colorbar(im1, ax=ax, label='Count')
cbar2 = fig.colorbar(im2, ax=ax, label='Score')


ax.set_xlim(-0.5,1.5)
ax.set_xticks([0,1])
ax.set_xticklabels(['Count','Score'])
ax.set_yticks(range(N))
ax.set_yticklabels(df['Country'])
ax.set_ylabel('Countries')
fig.tight_layout()
plt.show()

在此处输入图像描述

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

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