Seaborn热图(按列) [英] Seaborn heatmap by column

查看:133
本文介绍了Seaborn热图(按列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,热图功能完全应用于数据框.如果我只希望将热图应用于数据集中给定的一组列,该怎么办?我想这可以通过巧妙地使用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.

推荐答案

将所需的子DataFrame传递到

Pass the desired sub-DataFrame to seaborn.heatmap:

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

df[[col1, col2, ..., coln]]返回由df中的列col1col2,... 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆