为相关性矩阵绘制,颜色取值的最大值0.8,使每个单元格为方形的热力图
时间: 2024-03-16 11:45:50 浏览: 221
好的,为了绘制热力图,我们可以使用Python中的matplotlib库和seaborn库。具体代码如下:
``` python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 读取数据并进行预处理
data = pd.read_csv('weather_data.csv')
data = data.dropna() # 删除缺失值
# 选取需要计算相关性的列
cols = ['pm2.5', 'DEWP', 'TEMP', 'PRES', 'Iws', 'Is', 'Ir', 'Cbwd-NE', 'Cbwd-NW', 'Cbwd-SE', 'Cbwd-cv']
data_selected = data[cols]
# 计算相关性矩阵
corr_matrix = np.corrcoef(data_selected.T)
# 绘制热力图
sns.set(font_scale=1.2)
sns.heatmap(corr_matrix, vmax=0.8, square=True, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.xlabel('Features')
plt.ylabel('Features')
plt.show()
```
在以上代码中,我们使用了sns.heatmap()函数来绘制热力图,其中vmax参数表示颜色取值的最大值,square参数表示每个单元格是否为正方形,annot参数表示是否在每个单元格中显示数值,cmap参数表示使用的颜色映射。运行以上代码后,会得到一个热力图,其中每个单元格的颜色表示对应指标之间的相关性大小,颜色越深表示相关性越强。
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![xlsx](https://img-home.csdnimg.cn/images/20241231045042.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![m](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)