赞
踩
利用heatmap绘制协方差矩阵是数据可视化中常见的操作,而对颜色的选取则是一种艺术了。在不同的场景下有可能我们需要不同的色调或者颜色的搭配。而seaborn中的heatmap函数为我们提供了便捷。
Seaborn中有非常多的颜色选项可以选择,这里将效果一一亲测。数据如下:这里我们用最为简单的数据绘制协方差矩阵的图。
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt sns.set() data=pd.DataFrame({'A':[1,4,5,2,5,6,3,5,6,3,3], 'B':[4,3,7,3,5,2,4,3,5,5,2], 'C':[5,8,9,3,5,7,3,5,3,4,4], 'D':[5,4,3,6,7,3,5,2,6,6,4], 'E':[4,5,7,3,6,3,2,4,5,2,1], 'F':[7,6,4,7,4,7,9,3,2,2,3], 'G':[4,5,2,5,8,9,1,2,4,4,3], 'H':[6,4,2,6,2,6,1,3,8,9,6], 'I':[4,3,5,2,7,8,3,4,2,5,3], 'J':[4,2,5,7,8,4,5,2,5,1,2], 'K':[4,5,8,2,3,1,4,5,8,3,2]}) f, ax = plt.subplots(figsize=(13,11)) sns.heatmap(data.corr(), annot=True, ax=ax) ax.set_title('default')
Text(0.5, 1, 'default')
可以看到,这就是heatmap的默认颜色模式,除了这种模式,我们可以指定cmap的值,使得绘制出来的图像呈现不同的颜色(在字符串后面加’_r’则进行反向)
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Accent')
ax.set_title('Accent')
Text(0.5, 1, 'Accent')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Blues')
ax.set_title('Blues')
Text(0.5, 1, 'Blues')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='BrBG')
ax.set_title('BrBG')
Text(0.5, 1, 'BrBG')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='BuGn')
ax.set_title('BuGn')
Text(0.5, 1, 'BuGn')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='BuPu')
ax.set_title('BuPu')
Text(0.5, 1, 'BuPu')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='CMRmap')
ax.set_title('CMRmap')
Text(0.5, 1, 'CMRmap')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Dark2')
ax.set_title('Dark2')
Text(0.5, 1, 'Dark2')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='GnBu')
ax.set_title('GnBu')
Text(0.5, 1, 'GnBu')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Greens')
ax.set_title('Greens')
Text(0.5, 1, 'Greens')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Greys')
ax.set_title('Greys')
Text(0.5, 1, 'Greys')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='OrRd')
ax.set_title('OrRd')
Text(0.5, 1, 'OrRd')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Oranges')
ax.set_title('Oranges')
Text(0.5, 1, 'Oranges')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='PRGn')
ax.set_title('PRGn')
Text(0.5, 1, 'PRGn')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Paired')
ax.set_title('Paired')
Text(0.5, 1, 'Paired')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Pastel1')
ax.set_title('Pastel1')
Text(0.5, 1, 'Pastel1')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Pastel2')
ax.set_title('Pastel2')
Text(0.5, 1, 'Pastel2')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='PiYG')
ax.set_title('PiYG')
Text(0.5, 1, 'PiYG')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='PuBu')
ax.set_title('PuBu')
Text(0.5, 1, 'PuBu')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='PuBuGn')
ax.set_title('PuBuGn')
Text(0.5, 1, 'PuBuGn')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='PuOr')
ax.set_title('PuOr')
Text(0.5, 1, 'PuOr')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='PuRd')
ax.set_title('PuRd')
Text(0.5, 1, 'PuRd')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Purples')
ax.set_title('Purples')
Text(0.5, 1, 'Purples')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='RdBu')
ax.set_title('RdBu')
Text(0.5, 1, 'RdBu')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='RdGy')
ax.set_title('RdGy')
Text(0.5, 1, 'RdGy')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='RdPu')
ax.set_title('RdPu')
Text(0.5, 1, 'RdPu')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='RdYlBu')
ax.set_title('RdYlBu')
Text(0.5, 1, 'RdYlBu')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='RdYlGn')
ax.set_title('RdYlGn')
Text(0.5, 1, 'RdYlGn')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Reds')
ax.set_title('Reds')
Text(0.5, 1, 'Reds')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Set1')
ax.set_title('Set1')
Text(0.5, 1, 'Set1')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Set2')
ax.set_title('Set2')
Text(0.5, 1, 'Set2')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Set3')
ax.set_title('Set3')
Text(0.5, 1, 'Set3')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Spectral')
ax.set_title('Spectral')
Text(0.5, 1, 'Spectral')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='Wistia')
ax.set_title('Wistia')
Text(0.5, 1, 'Wistia')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='YlGn')
ax.set_title('YlGn')
Text(0.5, 1, 'YlGn')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='YlGnBu')
ax.set_title('YlGnBu')
Text(0.5, 1, 'YlGnBu')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='YlOrBr')
ax.set_title('YlOrBr')
Text(0.5, 1, 'YlOrBr')
f, ax = plt.subplots(figsize=(13,11))
sns.heatmap(data.corr(), annot=True, ax=ax,cmap='YlOrRd')
ax.set_title('YlOrRd')
Text(0.5, 1, 'YlOrRd')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。