Correlation matrix plot with coefficients on one side, scatterplots on another, and distributions on diagonal

后端 未结 2 1769
[愿得一人]
[愿得一人] 2020-12-05 01:27

I love this correlation matrix from the PerformanceAnalytics R package\'s chart.Correlation function:

How can I create this in Python? The corr

2条回答
  •  盖世英雄少女心
    2020-12-05 01:53

    The cor_matrix function below does this, plus adds a bivariate kernel density plot. Thanks to @karl-anka's comment for getting me started.

    import matplotlib.pyplot as plt
    import seaborn as sns
    from scipy import stats
    
    sns.set(style='white')
    iris = sns.load_dataset('iris')
    
    def corrfunc(x, y, **kws):
      r, p = stats.pearsonr(x, y)
      p_stars = ''
      if p <= 0.05:
        p_stars = '*'
      if p <= 0.01:
        p_stars = '**'
      if p <= 0.001:
        p_stars = '***'
      ax = plt.gca()
      ax.annotate('r = {:.2f} '.format(r) + p_stars,
                  xy=(0.05, 0.9), xycoords=ax.transAxes)
    
    def annotate_colname(x, **kws):
      ax = plt.gca()
      ax.annotate(x.name, xy=(0.05, 0.9), xycoords=ax.transAxes,
                  fontweight='bold')
    
    def cor_matrix(df):
      g = sns.PairGrid(df, palette=['red'])
      # Use normal regplot as `lowess=True` doesn't provide CIs.
      g.map_upper(sns.regplot, scatter_kws={'s':10})
      g.map_diag(sns.distplot)
      g.map_diag(annotate_colname)
      g.map_lower(sns.kdeplot, cmap='Blues_d')
      g.map_lower(corrfunc)
      # Remove axis labels, as they're in the diagonals.
      for ax in g.axes.flatten():
        ax.set_ylabel('')
        ax.set_xlabel('')
      return g
    
    cor_matrix(iris)
    

提交回复
热议问题