基本相关图

一个基本但说明性的热图,显示了许多变量之间的相关性。

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)

StackOverflow 文档

可能的改进:

  1. 删除 y 轴(Var A)和 x 轴(Var E)上的冗余标签
  2. 在地图的左下角或右上角添加相关性(值)