如何解决pairplot中的相关值
是否有一种方法可以显示seaborn.pairplot()
的成对相关值,如下面的示例(在ggpairs()
中用R
创建)?我可以使用随附的代码进行绘制,但是无法添加相关性。谢谢
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
g = sns.pairplot(iris,kind='scatter',diag_kind='kde')
# remove upper triangle plots
for i,j in zip(*np.triu_indices_from(g.axes,1)):
g.axes[i,j].set_visible(False)
plt.show()
解决方法
如果您使用PairGrid
而不是pairplot
,则可以传递一个自定义函数,该函数将计算相关系数并将其显示在图形上:
from scipy.stats import pearsonr
def reg_coef(x,y,label=None,color=None,**kwargs):
ax = plt.gca()
r,p = pearsonr(x,y)
ax.annotate('r = {:.2f}'.format(r),xy=(0.5,0.5),xycoords='axes fraction',ha='center')
ax.set_axis_off()
iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map_diag(sns.distplot)
g.map_lower(sns.regplot)
g.map_upper(reg_coef)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。