python - 相关矩阵图,一侧是系数,另一侧是散点图,对角线分布

标签 python pandas seaborn

我喜欢 PerformanceAnalytics R 包的 chart.Correlation function 中的这个相关矩阵:

PerformanceAnalytics chart.Correlation result

我如何在 Python 中创建它?我见过的相关矩阵图主要是热图,例如 this seaborn example .

最佳答案

另一种解决方案是

import matplotlib.pyplot as plt
import seaborn as sns

def corrdot(*args, **kwargs):
    corr_r = args[0].corr(args[1], 'pearson')
    corr_text = f"{corr_r:2.2f}".replace("0.", ".")
    ax = plt.gca()
    ax.set_axis_off()
    marker_size = abs(corr_r) * 10000
    ax.scatter([.5], [.5], marker_size, [corr_r], alpha=0.6, cmap="coolwarm",
               vmin=-1, vmax=1, transform=ax.transAxes)
    font_size = abs(corr_r) * 40 + 5
    ax.annotate(corr_text, [.5, .5,],  xycoords="axes fraction",
                ha='center', va='center', fontsize=font_size)

sns.set(style='white', font_scale=1.6)
iris = sns.load_dataset('iris')
g = sns.PairGrid(iris, aspect=1.4, diag_sharey=False)
g.map_lower(sns.regplot, lowess=True, ci=False, line_kws={'color': 'black'})
g.map_diag(sns.distplot, kde_kws={'color': 'black'})
g.map_upper(corrdot)

enter image description here


现在,如果您真的想要模仿那个 R 图的外观,您可以将上面的内容与您提供的一些解决方案结合起来:

import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
import numpy as np

def corrdot(*args, **kwargs):
    corr_r = args[0].corr(args[1], 'pearson')
    corr_text = round(corr_r, 2)
    ax = plt.gca()
    font_size = abs(corr_r) * 80 + 5
    ax.annotate(corr_text, [.5, .5,],  xycoords="axes fraction",
                ha='center', va='center', fontsize=font_size)

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(p_stars, xy=(0.65, 0.6), xycoords=ax.transAxes,
                color='red', fontsize=70)

sns.set(style='white', font_scale=1.6)
iris = sns.load_dataset('iris')
g = sns.PairGrid(iris, aspect=1.5, diag_sharey=False, despine=False)
g.map_lower(sns.regplot, lowess=True, ci=False,
            line_kws={'color': 'red', 'lw': 1},
            scatter_kws={'color': 'black', 's': 20})
g.map_diag(sns.distplot, color='black',
           kde_kws={'color': 'red', 'cut': 0.7, 'lw': 1},
           hist_kws={'histtype': 'bar', 'lw': 2,
                     'edgecolor': 'k', 'facecolor':'grey'})
g.map_diag(sns.rugplot, color='black')
g.map_upper(corrdot)
g.map_upper(corrfunc)
g.fig.subplots_adjust(wspace=0, hspace=0)

# Remove axis labels
for ax in g.axes.flatten():
    ax.set_ylabel('')
    ax.set_xlabel('')

# Add titles to the diagonal axes/subplots
for ax, col in zip(np.diag(g.axes), iris.columns):
    ax.set_title(col, y=0.82, fontsize=26)

enter image description here

这与 chart.Correlation() 在 R 中绘制 iris 数据集的方式非常接近:

library(PerformanceAnalytics)
chart.Correlation(data.matrix(iris[, -5]), histogram = TRUE, pch=20)

enter image description here

关于python - 相关矩阵图,一侧是系数,另一侧是散点图,对角线分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48139899/

相关文章:

python-3.x - Python - 基于不同列转置/透视一列

python - 使 Seaborn 热图之间的单元格大小相同

python - 替换已弃用的 tsplot

python - 如何使用变量作为标识符输入列表中的项目?

python - 如何基于周保存到 .csv 文件中

python - reshape Pandas 相关矩阵

python - 使用Python提取csv(数据框)中的特定字符串数据

python - for 循环中函数的子图

python - 为什么 Pandas Series.isin 适用于字符串而不适用于数字?

python - 尝试除了奇怪的行为 python 2.7