【问题标题】:Gnuplot: Plotting residualsGnuplot:绘制残差
【发布时间】:2013-01-09 10:20:33
【问题描述】:

我正在尝试绘制最小二乘法的可视化。最后它应该是这样的:

现在我有数据点和拟合曲线(在我的例子中是一条直线,称为 f(x))。我缺少的是残差(在链接中,残差是绿线)。 含义:我想从每个数据点开始画线,垂直于拟合线。

我的 data.txt 看起来像这样(缩写)

1, 3
2, 4
3, 6
4, 3

我的 gnuplot 命令是:

f(x) = a*x +b  + 1e-9
fit f(x) 'data.txt' u 1:2  via a, b
plot 'data.txt' with points title 'data',  f(x)  

到目前为止,我只设法将残差作为脉冲使用

plot 'data.txt' with points title 'data',  '' using 1:($2 - f($1)) w impulse, f(x)

但是像这样,从 y=0 开始向上向下,它们不在拟合线 f(x) 和数据点之间。

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    我想你在这里寻找errorbars

     plot 'data.txt' with points title 'data',\
          f(x) notitle,\
          'data.txt' u ($1):(f($1)):(f($1)):2 w yerrorbars title 'residuals'
    

    还有errorlines 样式,您可以使用基本相同的样式:

     plot 'data.txt' with points title 'data',\
          'data.txt' u ($1):(f($1)):(f($1)):2 w yerrorlines title 'residuals'
    

    但在这种情况下,线 (f(x)) 的绘制样式与绘制残差的样式相同。

    【讨论】:

    • 第一部分正是我想要的,谢谢!对其他有类似问题的人的附注:我必须使用 ...with points 4... 更改点的样式,以使这些点更好地与残差区分开来。
    • @albifant -- 我认为第一个是你想要的,但我包括了第二个,因为它看起来很接近并且可能对其他人有帮助。您可能希望更明确地说明您对这些点所做的更改。您可以更改点大小 (ps) 或点类型 (pt):... with points ps 2 pt 4
    • 我的印象是,当您省略 pt 而只选择 with points 4 时,它的行为就像隐含的 with points pt 4,但并不完全相同。生成的点样式实际上是相同的,但颜色却不同。所以这两个版本都可以工作,with points pt 4 更优雅。
    猜你喜欢
    • 1970-01-01
    • 2016-08-12
    • 2019-01-17
    • 2020-09-02
    • 2020-12-02
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多