Seaborn heatmap by column

2020-06-16 05:35发布

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.

1条回答
Viruses.
2楼-- · 2020-06-16 05:43

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

查看更多
登录 后发表回答