且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Seaborn热图(按列)

更新时间:2023-01-28 11:43:49

将所需的子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()

收益