saing-idea
首发于saing-idea
python3.x-seaborn.heatmap随笔

python3.x-seaborn.heatmap随笔

【seaborn.heatmap整理】

用处:将数据绘制为颜色方格(编码矩阵)。

最近在学习Q-learning算法,遇到了seaborn.heatmap绘方格图,今天整理一下:

引用形式seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt='.2g', annot_kws=None, linewidths=0, linecolor='white', cbar=True, cbar_kws=None, cbar_ax=None, square=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, **kwargs)

data : 要显示的数据

ax = sns.heatmap(data)

vmin, vmax : 显示的数据值的最大和最小的范围

ax = sns.heatmap(data,vmin=0, vmax=1)

cmap : matplotlib颜色表名称或对象,或颜色列表,可选从数据值到色彩空间的映射。如果没有提供,默认设置

ax = sns.heatmap(data,vmin=0, vmax=1, cmap = 'GnBu')

center : 指定色彩的中心值

ax = sns.heatmap(data,vmin=0, vmax=1, cmap = 'GnBu', center=0.7)

robust : 如果“Ture”和“ vmin或” vmax不存在,则使用强分位数计算颜色映射范围,而不是极值。

ax = sns.heatmap(data,vmin=0, vmax=1, cmap = 'GnBu', center=0.5, 
                 robust=False) #Set1

annot : 如果为True,则将数据值写入每个单元格中

ax = sns.heatmap(data,vmin=0, vmax=1, cmap = 'GnBu', center=0.5, 
                 robust=False,annot=True) #Set1

fmt : 表格里显示数据的类型

fmt ='.0%'#显示百分比
fmt ='f' 显示完整数字 = fmt ='g'
fmt ='.3'显示小数的位数 = fmt ='.3f' = fmt ='.3g'

annot_kws : 未知:

linewidths : 划分每个单元格的线的宽度。

linecolor : 划分每个单元格的线的颜色。

cbar : 是否绘制颜色条:colorbar,默认绘制

cbar_kws : 未知 cbar_ax : 显示x-y坐标,而不是节点的编号
square : 为‘True’时,整个网格为一个正方形

xticklabels, yticklabels : 可以以字符串进行命名,也可以调节编号的间隔,也可以不显示坐标

ax = sns.heatmap(uniform_data,cmap = 'RdBu', center=0,cbar = True, square = False,
                 xticklabels =['12','22'])#字符串命名
ax = sns.heatmap(uniform_data,cmap = 'RdBu', center=0,cbar = True, square = False,
                 xticklabels =2)#编号间隔为2
ax = sns.heatmap(uniform_data,cmap = 'RdBu', center=0,cbar = True, square = False,
                 xticklabels =False)#不显示坐标

mask : boolean array or DataFrame, optional
If passed, data will not be shown in cells where mask is True. Cells with missing values are automatically masked.
ax : matplotlib Axes, optional
Axes in which to draw the plot, otherwise use the currently-active Axes.
kwargs : other keyword arguments
All other keyword arguments are passed to ax.pcolormesh.
ax : matplotlib Axes
Axes object with the heatmap.

举例说明:
绘制一个numpy数组的热图:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)

更改色彩图的限制:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, vmin=0, vmax=1)

以0为中心的数据绘制热图:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
normal_data = np.random.randn(10, 12)
ax = sns.heatmap(normal_data, center=0)

用有意义的行和列标签绘制数据框:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights)

使用整数格式用数值注释每个单元格:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights, annot=True, fmt="d")

在每个单元格之间添加行:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights, linewidths=.5)

使用不同的颜色表:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights, cmap="YlGnBu")

以特定值居中色彩图:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights, center=flights.loc["January", 1955])

绘制每个其他列标签并且不要绘制行标签:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
data = np.random.randn(50, 20)
ax = sns.heatmap(data, xticklabels=2, yticklabels=False)

不要绘制颜色条:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
data = np.random.randn(10, 10)
ax = sns.heatmap(data, cbar=False)

对色条使用不同的轴:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
grid_kws = {"height_ratios": (.9, .05), "hspace": .3}
f, (ax, cbar_ax) = plt.subplots(2, gridspec_kw=grid_kws)
ax = sns.heatmap(flights, ax=ax,cbar_ax=cbar_ax,cbar_kws={"orientation": "horizontal"})

使用掩码只绘制矩阵的一部分:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()

corr = np.corrcoef(np.random.randn(10, 200))
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style("white"):
    ax = sns.heatmap(corr, mask=mask, vmax=.3, square=True)


import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
data = np.random.rand(10, 12)
f, ax = plt.subplots(figsize=(8,5))
ax = sns.heatmap(data,cmap = 'RdBu',ax=ax,vmin=0, vmax=1,annot=True,fmt ='0.1g')

#设置坐标字体方向
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation=45, horizontalalignment='right')
label_x = ax.get_xticklabels()
plt.setp(label_x, rotation=45, horizontalalignment='right')
plt.xlabel('x.num')#设置坐标名称
plt.ylabel('y.num')
plt.title('Plotting')#标题
plt.show()

参考资料:


seaborn.heatmap - seaborn 0.8.1 documentationseaborn.pydata.org图标

编辑于 2018-04-20

文章被以下专栏收录