# Vlines ymin, ymax

**URL:** <https://discourse.julialang.org/t/vlines-ymin-ymax/54671>\
**Category:** Visualization\
**Created:** [February 5, 2021, 6:08am UTC](https://discourse.julialang.org/t/vlines-ymin-ymax/54671 "2021-02-05T06:08:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [February 5, 2021, 6:08am UTC](https://discourse.julialang.org/t/vlines-ymin-ymax/54671/1 "2021-02-05T06:08:46Z")

</div>

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:

```julia
# 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

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [February 5, 2021, 6:55am UTC](https://discourse.julialang.org/t/vlines-ymin-ymax/54671/2 "2021-02-05T06:55:24Z")

</div>

Are your sure you are looking for [matplotlib’s `vlines`](https://matplotlib.org/3.3.4/gallery/lines_bars_and_markers/vline_hline_demo.html?highlight=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 `NaN`s. E.g.,

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

```

 ![Screen Shot 2021-02-05 at 6.02.10 pm](https://global.discourse-cdn.com/julialang/original/3X/1/1/11dc76609a07a662573ebb839f65d7c3c933a8a6.jpeg)

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [February 5, 2021, 7:17am UTC](https://discourse.julialang.org/t/vlines-ymin-ymax/54671/3 "2021-02-05T07:17:01Z")

</div>

> [@briochemc](#):
>
> ```julia
> x2 = repeat(x, inner=3)
> y2 = reduce(vcat, [y, yhat, NaN] for (y, yhat) in zip(y, yhat))
> plot!(x2, y2)
> 
> ```

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!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 5, 2021, 3:53pm UTC](https://discourse.julialang.org/t/vlines-ymin-ymax/54671/4 "2021-02-05T15:53:42Z")

</div>

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

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/c/4/c4597fa1524a469698c90371354c65d6e602d4ba.png)

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [February 8, 2021, 12:28pm UTC](https://discourse.julialang.org/t/vlines-ymin-ymax/54671/5 "2021-02-08T12:28:50Z")

</div>

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