【问题标题】:Seaborn regression plot with different colors不同颜色的 Seaborn 回归图
【发布时间】:2018-05-04 13:36:30
【问题描述】:

我想使用 Seaborn 绘制适合我的数据集的线性回归模型。

由于该数据集在水柱中具有不同的深度(底部、中间和顶部),我希望我的绘图具有 3 种不同的颜色,但线性回归将针对整个数据集。我划分了这个数据集以分别绘制它们,如下所示:

fig, axarr = plt.subplots(3, sharex = True)
axarr[0].scatter(averageOBSB_3min,PumpBotSSC,c='r', label='Bottom')
axarr[0].scatter(averageOBSM_3min,PumpMidSSC,c='g', label='Middle')
axarr[0].scatter(averageOBST_3min,PumpTopSSC,c='b', label='Top')

但这显然不适用于 Seaborn。

我的问题是:如何在绘图上有不同的颜色,但要对孔数据集进行回归?

【问题讨论】:

    标签: python regression seaborn


    【解决方案1】:

    混合使用 Seaborn 的 lmplot 和 regplot:

    import seaborn as sns
    #Use lmplot to plot scatter points
    graph = sns.lmplot(x='x_value', y='y_value', hue='water_value', data=df, fit_reg=False)
    #Use regplot to plot the regression line for the whole points
    sns.regplot(x='x_value', y='y_value', data=df, scatter=False, ax=graph.axes[0, 0]))
    

    【讨论】:

    • 谢谢。当我使用 lmplot 绘图时,会显示回归系数,但使用 regplot 则不会。有什么办法吗?
    【解决方案2】:

    这是@O.Suleiman 回答和您的评论的扩展。 Seaborn lmplot 和 regplot 有一种方法可以直接用其参数注释您的合身度。为此,您应该检查此post。总结这两个解决方案,由于您没有提供任何数据,我使用 seaborn 数据集,您将使用的是:

    import seaborn as sns
    from scipy import stats
    
    # get coeffs of linear fit
    slope, intercept, r_value, p_value, std_err = stats.linregress(tips['total_bill'],tips['tip'])
    
    # Use lmplot to plot scatter points
    sns.lmplot(x='total_bill', y='tip', hue='smoker', data=tips, fit_reg=False, legend=False)
    
    # Use regplot to plot the regression line and use line_kws to set line label for legend
    ax = sns.regplot(x="total_bill", y="tip", data=tips, scatter_kws={"zorder":-1},
    line_kws={'label':"y={0:.1f}x+{1:.1f}".format(slope,intercept)})
    
    # plot legend
    ax.legend()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 2017-11-02
      • 2018-06-17
      相关资源
      最近更新 更多