可视化 | Seaborn中的矩阵图及示例

Seaborn是python提供的一个很棒的可视化库。它有几种类型的绘图,通过这些绘图,它提供了惊人的可视化能力。其中一些包括计数图,散点图,配对图,回归图,矩阵图等等。本文讨论了Seaborn中的矩阵图。

示例1: 热图

热图是一种显示某种矩阵图的方法。要使用热图,数据应采用矩阵形式。矩阵的意思是索引名和列名必须以某种方式匹配,这样我们填充在单元格中的数据才是相关的。让我们看一个例子来更好地理解这一点。

# import the necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt % matplotlib inline 
 
# load the tips dataset
dataset = sns.load_dataset('tips')
 
# first five entries of the tips dataset
dataset.head()
 
# correlation between the different parameters
tc = dataset.corr()
 
# plot a heatmap of the correlated data
sns.heatmap(tc)

数据集前5条数据:

在这里插入图片描述
关系矩阵:
在这里插入图片描述

关系矩阵的热图:

在这里插入图片描述

为了获得更好的热图可视化,我们可以添加annot,线宽和线条颜色等参数。


# import the necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt % matplotlib inline
 
# load the tips dataset
dataset = sns.load_dataset('tips')
 
# first five entries of the tips dataset
dataset.head()
 
# correlation between the different parameters
tc = dataset.corr()
sns.heatmap(tc, annot = True, cmap ='plasma', 
            linecolor ='black', linewidths = 1)

说明:

  • annot用于注释属于这些单元格的实际值
  • cmap用于你想要的颜色映射,如coolwarm,plasma,magma等。
  • linewidth用于设置分隔单元格的线的宽度。
  • linecolor用于设置分隔单元格的线条的颜色。

下面是一个显示这些属性的图。

在这里插入图片描述
所以我们可以说,热图所做的就是根据梯度为单元格着色,并使用一些参数来增加数据的可视化。

示例2:聚类图

聚类图使用层次聚类。它根据行和列的相似性执行聚类。

# import the necessary libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt % matplotlib inline
 
# load the flights dataset
fd = sns.load_dataset('flights')
 
# make a dataframe of the data 
df = pd.pivot_table(values ='passengers', index ='month', 
                    columns ='year', data = fd)
 
# first five entries of the dataset
df.head()
 
# make a clustermap from the dataset
sns.clustermap(df, cmap ='plasma')

在这里插入图片描述
使用数据透视表创建的矩阵(前五个条目)

在这里插入图片描述

聚类图

在这里插入图片描述
我们还可以通过使用standard_scale参数来更改颜色条的比例。

# import the necessary libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt % matplotlib inline
 
# load the flights dataset
fd = sns.load_dataset('flights')
 
# make a dataframe of the data 
df = pd.pivot_table(values ='passengers', 
                    index ='month', columns ='year', data = fd)
 
# first five entries of the dataset
df.head()
 
# make a clustermap from the dataset
sns.clustermap(df, cmap ='plasma', standard_scale = 1)

在这里插入图片描述
standard_scale = 1标准化从0到1范围的数据。我们可以看到,月份和年份不再按顺序排列,因为它们是根据聚类图的相似性进行聚类的。

因此,我们可以得出结论,热图将按照我们给予的顺序显示事物,而聚类图则根据相似性对数据进行聚类。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python,可以使用numpy和matplotlib库来可视化相关矩阵。下面是两种常见的方法: 方法一:使用numpy和seaborn库 首先,使用numpy的corrcoef函数计算行与行之间的相关系数或列与列之间的相关系数。例如,可以使用np.corrcoef(a)计算行与行之间的相关系数,使用np.corrcoef(a,rowvar=0)计算列与列之间的相关系数,其a是一个二维数组。接下来,可以使用seaborn库的heatmap函数绘制热力图来可视化相关矩阵。heatmap函数可以设置颜色映射、是否显示数值以及矩阵小块间隔等参数。下面是一个示例代码: ```python import numpy as np import seaborn as sns a = np.array([[1, 1, 2, 2, 3], [2, 2, 3, 3, 5], [1, 4, 2, 2, 3]]) corr = np.corrcoef(a) sns.heatmap(corr, cmap='Blues', annot=True) ``` 方法二:使用numpy和matplotlib库 类似地,可以使用numpy的corrcoef函数计算相关系数矩阵。然后,可以使用matplotlib库的imshow函数绘制热力图。可以使用imshow函数设置颜色映射、是否显示数值以及矩阵小块间隔等参数。下面是一个示例代码: ```python import numpy as np import matplotlib.pyplot as plt a = np.array([[1, 1, 2, 2, 3], [2, 2, 3, 3, 5], [1, 4, 2, 2, 3]]) corr = np.corrcoef(a) plt.imshow(corr, cmap='Blues') plt.colorbar() plt.show() ``` 这两种方法都能够将相关矩阵可视化为热力图,以帮助我们直观地理解相关性信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [相关系数矩阵与热力图heatmap(Python高级可视化seaborn)](https://blog.csdn.net/cymy001/article/details/79576019)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [python seaborn heatmap可视化相关性矩阵](https://blog.csdn.net/sjtulgl/article/details/92796547)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值