在 pandas 的散布矩阵中添加回归线 [英] Add regression line in the scatter matrix of pandas
本文介绍了在 pandas 的散布矩阵中添加回归线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现了很多有关此主题的新闻,但没有人提出我的理由.我有一个相当大的数据框,我想在其中添加回归线,并在网格的另一侧仅将相关系数放在空白处.
I've found a lot of news on this subject, but no one has made my case. I have a quite large dataframe where I would like to add the regression line and on the opposite side of the grid put only the correlation coefficient in the empty spaces.
df=pd.DataFrame(np.concatenate(Arr))
df
a b c d e f g h
0 94.122932 87.930649 57.192429 35.844883 57.971062 65.494003 52.297470 52.553162
1 92.231049 87.693893 53.804562 33.005547 52.124733 56.096642 48.072334 46.176899
2 89.846649 87.448158 49.858879 29.900572 46.716476 44.890785 44.026333 40.420742
3 87.181229 87.291374 46.363262 27.649641 41.478992 36.512981 40.489635 35.537495
4 85.915497 87.230659 43.459812 25.325624 37.368202 30.755083 37.228760 31.470888
...
axes = pd.plotting.scatter_matrix(df)
for i in range(np.shape(axes)[0]):
for j in range(np.shape(axes)[1]):
if i < j:
axes[i,j].set_visible(False)
你如何添加它?
推荐答案
最简单的方法是使用seaborn的PairGrid
:
The simplest way would be to use seaborn's PairGrid
:
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)
这篇关于在 pandas 的散布矩阵中添加回归线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文