Matplotlib/Seaborn:控制热图的行高/行高 [英] Matplotlib / Seaborn: control line/row height of heatmap

查看:112
本文介绍了Matplotlib/Seaborn:控制热图的行高/行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过调用 sns.heatmap() 使用 seaborn 生成热图.每个单元格的颜色基于行百分比,我想控制每行/行的高度.

I am producing a heatmap using seaborn by calling sns.heatmap(). The color of each cell is based on row percentages and I'd like to control the height of each row/line.

为了说明,这是一个具有相等线高的热图:

To illustrate, here is a heatmap with equal line heights:

该映射包含每一行的百分比值.我想根据基础计数的行总和设置每一行的高度,以说明每一行的重要性.

The map contains percentage values for each row. I'd like to set the height of each row according to row sums of the underlying counts to illustrate the importance of each row.

代码:

pcts = data.apply(lambda x: x / float(x.sum()), axis=1)
sns.heatmap(data)

推荐答案

使用 seaborn.heatmap 无法创建不同大小的行.但是当然您可以使用 matplotlib 创建热图.这将涉及创建具有所需间距的网格并在该网格上绘制值.

Creating different sized rows is not possible with seaborn.heatmap. But of course you can create the heatmap using matplotlib. This would involve creating a grid with the desired spacings and plot the values on that grid.

import numpy as np;np.random.seed(1)
import matplotlib.pyplot as plt

# get some data
a = np.random.rayleigh(3,111)
h,_ = np.histogram(a)
data = np.r_[[h]*10].T+np.random.rand(10,10)*11

# produce scaling for data
y = np.cumsum(np.append([0],np.sum(data, axis=1)))
x = np.arange(data.shape[1]+1)
X,Y = np.meshgrid(x,y)
# plot heatmap
im = plt.pcolormesh(X,Y,data)

# set ticks
ticks = y[:-1] + np.diff(y)/2
plt.yticks(ticks, np.arange(len(ticks)))
plt.xticks(np.arange(data.shape[1])+0.5,np.arange(data.shape[1]))
# colorbar
plt.colorbar(im)

plt.show()

这篇关于Matplotlib/Seaborn:控制热图的行高/行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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