【问题标题】:Add regression line in the scatter matrix of pandas在 pandas 的散点矩阵中添加回归线
【发布时间】:2020-04-07 02:47:05
【问题描述】:

我发现了很多关于这个主题的新闻,但没有人提出我的观点。 我有一个相当大的数据框,我想在其中添加回归线,并在网格的另一侧仅将相关系数放在空白处。

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)

如何添加?

【问题讨论】:

    标签: python pandas matplotlib scatter-matrix


    【解决方案1】:

    最简单的方法是使用 seaborn 的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)
    

    【讨论】:

    • 非常感谢您的回复,对我的“df”进行了一些测试,我注意到当我将轴设置在 0 到 255 的范围内时,点云的图像质量损失很大. 另外,我不知道如何改变每个散点图的颜色。
    猜你喜欢
    • 1970-01-01
    • 2015-01-27
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2022-11-13
    相关资源
    最近更新 更多