目录
Python数据处理从零开始----第四章(可视化)①③多变量绘图
Python数据处理从零开始----第四章(可视化)(14)使用seaborn绘制热图
seaborn.heatmapHeat maps显示数字表格数据,其中单元格根据包含的值着色。 热图非常适合使这种数据的趋势更加明显,特别是在订购数据并且存在聚类时。
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'
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)#不显示坐标
举例说明:
绘制一个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)
image
更改色彩图的限制:
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)
image
以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)
image
用有意义的行和列标签绘制数据框:
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)
image
使用整数格式用数值注释每个单元格:
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")
image
在每个单元格之间添加行:
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)
image
使用不同的颜色表:
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")
image
以特定值居中色彩图:
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])
image
绘制每个其他列标签并且不要绘制行标签:
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)
image
不要绘制颜色条:
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)
image
对色条使用不同的轴:
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"})
image
使用掩码只绘制矩阵的一部分:
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)
image
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()
image