Vlines ymin, ymax

Hi,

I am using Plots with gr() backend and want to plot the errors of a linear regression model, i.e. indicate with vertical lines the difference between y and yhat.

Essentially, I am looking for something similar to python’s matplotlib vlines. Using vline does not work as I cannot specify ymin and ymax.

Here is my code for the data and the regression line:

# data
n = 30
paras = Dict(:b0 => 6, :b1 => 3.2)
x = range(0,1; length=n) .* 10
e = rand(Normal(0,mean(x)/3),n) 
y = paras[:b0] .+ paras[:b1] .* x .+ e

# plot
calc_line(x, paras) = paras[:b0] .+ paras[:b1] .* x 
yhat = calc_line(x, Dict(:b0=>a, :b1=>b))
scatter(x, y)
plot!(x, yhat)

vlines # Should plot vertical lines for difference between yhat and y

How can I do this using gr(). I don’t want to use pyplot given it does not run stable on my computer (a separate issue I need to fix). Also I prefer not to use another backend if possible.

I would appreciate any hint!

Thanks

Are your sure you are looking for matplotlib’s vlines equivalent? Because that’s exactly Plots.jl’s vline, but that will not give you the difference between y and yhat AFAIK.

(Side note: your MWE does not run because a and b are not defined.)


Edit: OK I think I know what you mean. You want to plot segments from y to yhat. You can do that by manually plotting each segment. One way is put all the coordinates in a vector and separate the segments with NaNs. E.g.,

x2 = repeat(x, inner=3)
y2 = reduce(vcat, [y, yhat, NaN] for (y, yhat) in zip(y, yhat))
plot!(x2, y2)

2 Likes

GREAT! Thanks for the swift help. I was actually playing around with zip(y,yhat) yesterday but it didn’t work as I would have never thought including NaNs to make it working.

Really cool. Thanks for your swift help!

1 Like

A different approach might be to use GR’s plot keywords yerror (and ribbon):

err = y - yhat; z0 = 0*err;
plot(x,yhat, label=str, lw=2, c=:green, yerror=(z0,err), ribbon=(z0,err), fc=:cyan, fa=0.1)
scatter!(x, y, mc=:red, label="Input data", legend=:topleft)

linear_regression

4 Likes

Great, thanks for making me aware of yerror. Thanks!