本文介绍了相关矩阵图,一侧为系数,另一侧为散点图,对角线分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢PerformanceAnalytics
R包的 chart.Correlation
函数:
I love this correlation matrix from the PerformanceAnalytics
R package's chart.Correlation
function:
如何在Python中创建它?我见过的相关矩阵图主要是热图,例如此seaborn
示例
How can I create this in Python? The correlation matrix plots I've seen are primarily heatmaps, such as this seaborn
example.
推荐答案
下面的cor_matrix
函数可以执行此操作,并添加一个双变量内核密度图.感谢@ karl-anka的评论让我入门.
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)
这篇关于相关矩阵图,一侧为系数,另一侧为散点图,对角线分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!