seaborn heatmap可视化模式亲测
利用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’则进行反向)
1 Accent
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')

2 Blues
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')

3 BrBG
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')

4 BuGn
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')

5 BuPu
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')

6 CMRmap
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')

7 Dark2
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')

8 GnBu
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')

9 Greens
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')

10 Greys
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')

11 OrRd
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')

12 Oranges
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')

13 PRGn
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')

14 Paired
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')

15 Pastel1
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')

16 Pastel2
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')

17 PiYG
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')

18 PuBu
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')

19 PuBuGn
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')

20 PuOr
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')

21 PuRd
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')

22 Purples
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')

23 RdBu
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')

24 RdGy
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')

25 RdPu
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')

26 RdYlBu
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')

27 RdYlGn
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')

28 Reds
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')

29 Set1
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')

30 Set2
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')

31 Set3
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')

32 Spectral
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')

33 Wistia
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')

34 YlGn
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')

35 YlGnBu
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')

36 YlOrBr
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')

37 YlOrRd
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')

本文通过实际案例,展示了如何使用Seaborn库中的heatmap函数绘制协方差矩阵,并详细介绍了37种不同的颜色模式,帮助读者在数据可视化中选择最合适的颜色方案。

2899

被折叠的 条评论
为什么被折叠?



