46_Pandas,Python,Seaborn热图的生成
Python 的可视化库 seaborn 可以轻松创建可视化 2D 数据的热图。
使用 seaborn.heatmap() 函数。
Pandas 不是必须的,但是如果使用pandas.DataFrame 作为2D 数据,行列名会显示为x 轴和y 轴标签,很方便。
这里,将描述以下内容。
- seaborn.heatmap()函数的基本用法
- 作为对象操作
- seaborn.heatmap() 函数的主要参数
- 显示编号:annot
- 显示/隐藏颜色条:cbar
- 显示为正方形:square
- 指定最大值、最小值、中位数:vmax、vmin、center
- 指定颜色(颜色图):cmap
- 指定尺寸
导入以下库。
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
seaborn.heatmap()函数的基本用法
在seaborn.heatmap()的第一个参数data中指定要可视化的二维数组。
可以指定 Python 列表、numpy.ndarray 和 pandas.DataFrame 的二维数组。
对于 Python 列表的二维数组(列表列表)。
list_2d = [[0, 1, 2], [3, 4, 5]]
sns.heatmap(list_2d)
使用plt.savefig()保存为图像文件,plt.show()使用OS图像显示程序显示而不是保存文件。
创建重复图时注意,除非用plt.figure()生成新图或用plt.clf()初始化,否则之前的绘制结果可能会保留。
此外,如果生成多个数字(默认为 20 或更多),则会发出警告。在plt.savefig()或者plt.show()之后执行plt.close(‘all’)就可以了。
plt.figure()
sns.heatmap(list_2d)
plt.savefig('./data/46/seaborn_heatmap_list.png')
plt.close('all')
对于 numpy.ndarray。
arr_2d = np.arange(-8, 8).reshape((4, 4))
print(arr_2d)
# [[-8 -7 -6 -5]
# [-4 -3 -2 -1]
# [ 0 1 2 3]
# [ 4 5 6 7]]
plt.figure()
sns.heatmap(arr_2d)
plt.savefig('./data/46/seaborn_heatmap_ndarray.png')
对于 pandas.DataFrame。在 pandas.DataFrame 中,行名索引和列名列显示为 x 轴 / y 轴标签。
df = pd.DataFrame(data=arr_2d, index=['a', 'b', 'c', 'd'], columns=['A', 'B', 'C', 'D'])
print(df)
# A B C D
# a -8 -7 -6 -5
# b -4 -3 -2 -1
# c 0 1 2 3
# d 4 5 6 7
plt.figure()
sns.heatmap(df)
plt.savefig('./data/46/seaborn_heatmap_dataframe.png')
作为对象操作
seaborn.heatmap() 返回一个 Matplotlib AxesSubplot 对象。
print(type(sns.heatmap(list_2d)))
# <class 'matplotlib.axes._subplots.AxesSubplot'>
默认情况下,它在活动子图中绘制,但您可以使用 seaborn.heatmap() 的参数 ax 指定任何子图并绘制它。
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
sns.heatmap(list_2d, ax=ax)
fig.savefig('./data/46/seaborn_heatmap_list.png')
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(8, 6))
sns.heatmap(list_2d, ax=axes[0, 0])
sns.heatmap(arr_2d, ax=axes[1, 2])
fig.savefig('./data/46/seaborn_heatmap_list_sub.png')
seaborn.heatmap() 函数的主要参数
显示编号:annot
设置 annot = True 以在热图上显示值。
sns.heatmap(df, annot=True)
显示/隐藏颜色条:cbar
设置 cbar = False 以隐藏颜色条。
sns.heatmap(df, cbar=False)
显示为正方形:square
如果 square = True,则热图将绘制为正方形。
sns.heatmap(df, square=True)
指定最大值、最小值、中位数:vmax、vmin、center
热图的最大值、最小值和中值分别由 vmax、vmin 和 center 指定。
sns.heatmap(df, vmax=10, vmin=-10, center=0)
指定颜色(颜色图):cmap
颜色由 cmap 指定。您可以按原样使用可与 Matplotlib 一起使用的颜色图。
sns.heatmap(df, cmap='hot')
将 _r 添加到颜色图字符串会颠倒颜色的顺序。
sns.heatmap(df, cmap='Blues')
sns.heatmap(df, cmap='Blues_r')
指定尺寸
生成图像的大小由figsize(单位:英寸)和dpi(每英寸点数)决定。
figsize由plt.figure()或plt.subplots()的参数指定,dpi由savefig()的参数指定。
每个都可以按如下方式确认和更改。
current_figsize = mpl.rcParams['figure.figsize']
print(current_figsize)
# [6.0, 4.0]
plt.figure(figsize=(9, 6))
sns.heatmap(df, square=True)
plt.savefig('./data/46/seaborn_heatmap_big.png')
current_dpi = mpl.rcParams['figure.dpi']
print(current_dpi)
# 72.0
plt.figure()
sns.heatmap(df, square=True)
plt.savefig('./data/46/seaborn_heatmap_big_2.png', dpi=current_dpi * 1.5)