天天看點

matlab如何繪制相關系數熱力圖,相關系數矩陣與熱力圖heatmap

相關系數矩陣與熱力圖heatmap

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

# 生成随機矩陣

df = np.random.randint(-10,10,size=(5,8))

df

array([[-10, -4, 1, 8, 8, -9, -2, -2],

[ -4, -2, 7, -10, -6, 4, 5, -1],

[ 6, 4, 1, -7, 0, 3, 3, 4],

[ 8, -2, 9, -4, 0, 1, 2, 0],

[ 3, 5, -3, 1, -4, -3, 8, 2]])

# 計算相關系數矩陣

corr = np.corrcoef(df)

corr

array([[ 1. , -0.45187846, -0.81601955, -0.41843364, -0.29089038],

[-0.45187846, 1. , 0.4896793 , 0.54083859, 0.02788629],

[-0.81601955, 0.4896793 , 1. , 0.48284327, 0.2963696 ],

[-0.41843364, 0.54083859, 0.48284327, 1. , -0.14536089],

[-0.29089038, 0.02788629, 0.2963696 , -0.14536089, 1. ]])

# 生成熱力圖

f,ax = plt.subplots(figsize=(10,5))

sns.heatmap(corr,annot=True)

# annot=True表示在方格内顯示數值。

matlab如何繪制相關系數熱力圖,相關系數矩陣與熱力圖heatmap

相關系數矩陣熱力圖

fig,ax = plt.subplots()等價于:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

fig, ax = plt.subplots(1,3),其中參數1和3分别代表子圖的行數和列數,一共有 1x3 個子圖像。函數傳回一個figure圖像和子圖ax的array清單。

fig, ax = plt.subplots(1,3,1),最後一個參數1代表第一個子圖。

如果想要設定子圖的寬度和高度可以在函數内加入figsize值

fig, ax = plt.subplots(1,3,figsize=(15,7)),這樣就會有1行3個15x7大小的子圖。