⼀、python可视化——热⼒图
热⼒图
1、利⽤热⼒图可以看数据表⾥多个特征两两的相似度。参考官⽅API参数及地址:
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)
(1)热⼒图输⼊数据参数:
data:矩阵数据集,可以是numpy的数组(array),也可以是pandas的DataFrame。如果是DataFrame,则df的index/column信息会分别对应到heatmap的columns和rows,即pt.index是热⼒图的⾏标,pt.columns是热⼒图的列标
(2)热⼒图矩阵块颜⾊参数:
vmax,vmin:分别是热⼒图的颜⾊取值最⼤和最⼩范围,默认是根据data数据表⾥的取值确定
cmap:从数字到⾊彩空间的映射,取值是matplotlib包⾥的colormap名称或颜⾊对象,或者表⽰颜⾊的列表;改参数默认值:根据center 参数设定
center:数据表取值有差异时,设置热⼒图的⾊彩中⼼对齐值;通过设置center值,可以调整⽣成的图像颜⾊的整体深浅;设置center数据时,如果有数据溢出,则⼿动设置的vmax、vmin会⾃动改变
robust:默认取值False;如果是False,且没设定vmin和vmax的值,热⼒图的颜⾊映射范围根据具有鲁棒性的分位数设定,⽽不是⽤极值设定
(3)热⼒图矩阵块注释参数:
annot(annotate的缩写):默认取值False;如果是True,在热⼒图每个⽅格写⼊数据;如果是矩阵,在热⼒图每个⽅格写⼊该矩阵对应位置数据
fmt:字符串格式代码,矩阵上标识数字的数据格式,⽐如保留⼩数点后⼏位数字
annot_kws:默认取值False;如果是True,设置热⼒图矩阵上数字的⼤⼩颜⾊字体,matplotlib包text类下的字体设置;官⽅⽂档:(4)热⼒图矩阵块之间间隔及间隔线参数:
linewidths:定义热⼒图⾥“表⽰两两特征关系的矩阵⼩块”之间的间隔⼤⼩
linecolor:切分热⼒图上每个矩阵⼩块的线的颜⾊,默认值是’white’
(5)热⼒图颜⾊刻度条参数:
cbar:是否在热⼒图侧边绘制颜⾊刻度条,默认值是True
cbar_kws:热⼒图侧边绘制颜⾊刻度条时,相关字体设置,默认值是None
cbar_ax:热⼒图侧边绘制颜⾊刻度条时,刻度条位置设置,默认值是None
(6)square:设置热⼒图矩阵⼩块形状,默认值是False
xticklabels, yticklabels:xticklabels控制每列标签名的输出;yticklabels控制每⾏标签名的输出。默认值是auto。如果是True,则以DataFrame的列名作为标签名。如果是False,则不添加⾏标签名。如果是列表,则标签名改为列表中给的内容。如果是整数K,则在图上每隔K个标签进⾏⼀次标注。 如果是auto,则⾃动选择标签的标注间距,将标签名不重叠的部分(或全部)输出
mask:控制某个矩阵块是否显⽰出来。默认值是None。如果是布尔型的DataFrame,则将DataFrame⾥True的位置⽤⽩⾊覆盖掉
ax:设置作图的坐标轴,⼀般画多个⼦图时需要修改不同的⼦图的该值
**kwargs:All other keyword arguments are passed to ax.pcolormesh
热⼒图矩阵块颜⾊参数
#cmap(颜⾊)
import matplotlib.pyplot as plt
% matplotlib inline
f, (ax1,ax2) = plt.subplots(figsize = (6,4),nrows=2)
# cmap⽤cubehelix map颜⾊
cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True) sns.heatmap(pt, linewidths = 0.05, ax = ax1, vmax=900, vmin=0, cmap=cmap)
ax1.set_title('cubehelix map')
ax1.set_xlabel('')
ax1.set_xticklabels([]) #设置x轴图例为空值
ax1.set_ylabel('kind')
# cmap⽤matplotlib colormap
sns.heatmap(pt, linewidths = 0.05, ax = ax2, vmax=900, vmin=0, cmap='rainbow')
# rainbow为 matplotlib 的colormap名称
ax2.set_title('matplotlib colormap')
ax2.set_xlabel('region')
ax2.set_ylabel('kind')
#center的⽤法(颜⾊)
f, (ax1,ax2) = plt.subplots(figsize = (6, 4),nrows=2)
cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True) sns.heatmap(pt, linewidths = 0.05, ax = ax1, cmap=cmap, center=None )
ax1.set_title('center=None')
ax1.set_xlabel('')
ax1.set_xticklabels([]) #设置x轴图例为空值
ax1.set_ylabel('kind')
# 当center设置⼩于数据的均值时,⽣成的图⽚颜⾊要向0值代表的颜⾊⼀段偏移
sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=200)
ax2.set_title('center=3000')
matplotlib中subplot
ax2.set_xlabel('region')
ax2.set_ylabel('kind')
热⼒图矩阵块注释参数#robust 的⽤法(颜⾊)
f, (ax1,ax2) = plt.subplots(figsize = (6,4),nrows=2)
cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True)
sns.heatmap(pt, linewidths = 0.05, ax = ax1, cmap=cmap, center=None, robust=False )
ax1.set_title('robust=False')
ax1.set_xlabel('')
ax1.set_xticklabels([]) #设置x 轴图例为空值
ax1.set_ylabel('kind')
sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=None, robust=True )
ax2.set_title('robust=True')
ax2.set_xlabel('region')
ax2.set_ylabel('kind')
#annot(矩阵上数字),annot_kws(矩阵上数字的⼤⼩颜⾊字体)matplotlib包text类下的字体设置
import numpy as np
np.random.seed(20180316)
x = np.random.randn(4, 4)
f, (ax1, ax2) = plt.subplots(figsize=(6,6),nrows=2)
sns.heatmap(x, annot=True, ax=ax1)
sns.heatmap(x, annot=True, ax=ax2, annot_kws={'size':9,'weight':'bold', 'color':'blue'})
# Keyword arguments when annot is True.  stackoverflow/questions/35024475/seaborn-heatmap-key-words
#fmt(字符串格式代码,矩阵上标识数字的数据格式,⽐如保留⼩数点后⼏位数字)
import numpy as np
np.random.seed(0)
x = np.random.randn(4,4)
f, (ax1, ax2) = plt.subplots(figsize=(6,6),nrows=2)
sns.heatmap(x, annot=True, ax=ax1)
sns.heatmap(x, annot=True, fmt='.1f', ax=ax2)
热⼒图矩阵块之间间隔及间隔线参数
#linewidths(矩阵⼩块的间隔),linecolor(切分热⼒图矩阵⼩块的线的颜⾊)
import matplotlib.pyplot as plt
f, ax = plt.subplots(figsize = (6,4))
cmap = sns.cubehelix_palette(start = 1, rot = 3, gamma=0.8, as_cmap = True)
sns.heatmap(pt, cmap = cmap, linewidths = 0.05, linecolor= 'red', ax = ax)
ax.set_title('Amounts per kind and region')
ax.set_xlabel('region')
ax.set_ylabel('kind')