Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn heatmap by column

It seems to me the heatmap function is applied to the dataframe in its entirety. What if I only want the heatmap applied to a given set of column(s) from my dataset? I would imagine this can be achieved by smartly using cmap, but cannot seem to get it to work.

like image 362
user4704759 Avatar asked Jun 25 '15 21:06

user4704759


1 Answers

Pass the desired sub-DataFrame to seaborn.heatmap:

seaborn.heatmap(df[[col1, col2]], ...)

df[[col1, col2, ..., coln]] returns a DataFrame composed of the columns col1, col2, ... coln from df. Note the double brackets.


If you wish to highlight only certain values and plot the heatmap as though all other values are zero, you could make a copy of the DataFrame and set those values to zero before calling heatmap. For example, modifying the example from the docs,

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import seaborn.matrix as smatrix

sns.set()

flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
flights = flights.reindex(flights_long.iloc[:12].month)

columns = [1953,1955]
myflights = flights.copy()
mask = myflights.columns.isin(columns)
myflights.loc[:, ~mask] = 0
arr = flights.values
vmin, vmax = arr.min(), arr.max()
sns.heatmap(myflights, annot=True, fmt="d", vmin=vmin, vmax=vmax)
plt.show()

yields

enter image description here

like image 93
unutbu Avatar answered Oct 04 '22 00:10

unutbu