Matplotlib / Seaborn: control line/row height of heatmap

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.

Code:

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

Upvotes: 1

Views: 1024

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339310

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()

enter image description here

Upvotes: 2

Related Questions