Seaborn热图(按列) [英] Seaborn heatmap by column
问题描述
在我看来,热图功能完全应用于数据框.如果我只希望将热图应用于数据集中给定的一组列,该怎么办?我想这可以通过巧妙地使用cmap来实现,但似乎无法使其正常工作.
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.
推荐答案
Pass the desired sub-DataFrame to seaborn.heatmap
:
seaborn.heatmap(df[[col1, col2]], ...)
df[[col1, col2, ..., coln]]
返回由df
中的列col1
,col2
,... coln
组成的DataFrame.请注意双括号.
df[[col1, col2, ..., coln]]
returns a DataFrame composed of the columns col1
, col2
, ... coln
from df
. Note the double brackets.
如果您只想突出显示某些值并绘制热图,就好像所有其他值都为零,
您可以复制DataFrame并将这些值设置为零,然后再调用heatmap
.例如,修改文档中的示例,
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()
收益
这篇关于Seaborn热图(按列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!