user4704759
user4704759

Reputation: 321

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.

Upvotes: 15

Views: 38593

Answers (1)

unutbu
unutbu

Reputation: 879421

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

Upvotes: 13

Related Questions