R Sem
R Sem

Reputation: 241

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. Here is how part of my dataframe looks (the full dataframe contains 244 rows):

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. Is it possible to create something like this using seaborn?

I have attached an example of how I'm hoping the Heatmap could look like:

enter image description here

Thanks

Upvotes: 1

Views: 3453

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

You can create your heatmap "by hand" fairly easily using imshow(). It is just a matter of placing the resulting image correctly in the axes using the extent= keyword.

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

enter image description here

Upvotes: 3

Related Questions