Column marine heatmap

It seems to me that the heatmap function applies to the data frame as a whole. What if I want only the heatmap to be applied to a given set of columns (columns) from my data set? I would suggest that this can be achieved thanks to the clever use of cmap, but doesn't seem to be able to make it work.

+5
source share
1 answer

Pass the desired sub-DataFrame to seaborn.heatmap :

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

df[[col1, col2, ..., coln]] returns a DataFrame consisting of columns col1 , col2 , ... coln from df . Pay attention to double brackets.


If you want to select only certain values ​​and build a graphics card, as if all other values ​​were zero, you can make a copy of the DataFrame and set these values ​​to zero before calling heatmap . For example, by changing the example from the documents ,

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

gives

enter image description here

+5
source

All Articles