基本相關圖
一個基本但說明性的熱圖,顯示了許多變數之間的相關性。
import pandas as pd
import seaborn as sns
import numpy as np
# Sample dataframe with date index and five variables
np.random.seed(123)
df = pd.DataFrame(np.random.uniform(-0.25,0.25,size=(5, 5)),
columns = ['Var A','Var B','Var C', 'Var D', 'Var E'])
df['Dates'] = pd.date_range(start = None, end = pd.datetime.today().strftime('%Y-%m-%d'),
periods=5).tolist()
df = df.set_index(['Dates'])
# Compute correlations
corr = df.corr()
# Exclude duplicate correlations by masking uper right values
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Set background color / chart style
sns.set_style(style = 'white')
# Set up matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))
# Add diverging colormap
cmap = sns.diverging_palette(10, 250, as_cmap=True)
# Draw correlation plot
sns.heatmap(corr, mask=mask, cmap=cmap,
square=True,
linewidths=.5, cbar_kws={"shrink": .5}, ax=ax)
可能的改進:
- 刪除 y 軸(Var A)和 x 軸(Var E)上的冗餘標籤
- 在地圖的左下角或右上角新增相關性(值)