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:
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
calc_line(x, paras) = paras[:b0] .+ paras[:b1] .* x
yhat = calc_line(x, Dict(:b0=>a, :b1=>b))
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.
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))