# How to specify the span of hline in Plots.jl

**URL:** <https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [April 18, 2021, 1:09pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539 "2021-04-18T13:09:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [April 18, 2021, 1:09pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/1 "2021-04-18T13:09:12Z")

</div>

How can I specify the range over which `hline` will span? In the code below, for example, I want hline to span the two vlines

```julia
using Plots

pyplot()

plot(xlims=(0,4))
vline!([1], color=:darkred, label="1")
vline!([2], color=:black, label="2")
hline!([2], linestyle=:dash)

```

I tried `hspan!`, but to no avail.

Thank you.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 18, 2021, 2:11pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/2 "2021-04-18T14:11:22Z")

</div>

You know both `x` and `y` coordinates at both ends, so you can `plot([x, x], [ymin, ymax])`

---

<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:** [April 18, 2021, 2:34pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/3 "2021-04-18T14:34:13Z")

</div>

The order in previous post seems to be reversed, if one wants horizontal lines. 🙂  
For multiple horizontal lines one can do:

```julia
y = [1.25 1.5 1.75 2] # y-values for multiple horizontal lines
x1, x2 = 1, 2 # x end points for horizontal lines
plot!([x1; x2], [y; y], lw=2, lc=:black, legend=false)

```

![Limited_span_horizontal_lines](https://global.discourse-cdn.com/julialang/original/3X/e/5/e589f08a63bbc5b4ac492cea19de646fd2ac6a11.png)

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [April 18, 2021, 3:38pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/4 "2021-04-18T15:38:01Z")

</div>

Thank you guys. I don’t know why I didn’t think of such a simple solution 😅

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 18, 2021, 3:40pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/5 "2021-04-18T15:40:37Z")

</div>

What’s wrong with `hspan!`?

```julia
julia> plot(rand(10))

julia> hspan!([0.1, 0.5])

```

![image](https://global.discourse-cdn.com/julialang/original/3X/d/c/dca3cf40b86f59d680f1c5e38b1b7269d4bfc8dc.png)

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [April 18, 2021, 3:50pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/6 "2021-04-18T15:50:41Z")

</div>

The problem in my particular use case is that the horizontal line spans the entire x-axis. Is there a way to plot hspan as a line between xmin and xmax?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 18, 2021, 3:52pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/7 "2021-04-18T15:52:16Z")

</div>

Ah my apologies, I didn’t read closely enough then - no I don’t believe that’s possible, `vspan` and `hspan` are specifically made for spanning the entire plot range vertically/hoirzontally

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [April 18, 2021, 4:05pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/8 "2021-04-18T16:05:02Z")

</div>

No problem!

For completeness, here is an adaption of the solution above for vertical lines. It draws a vertical line extending to the curve of a normal PDF to show the density at particular x values.

```julia
using Plots, Distributions
# add density lines
x = -3:.05:3
y = pdf.(Normal(0,1), x)
x_line = [.6,1,1.8]
plot(x, y)
density_max = pdf.(Normal(0,1), x_line)
density_min = fill(0.0, length(x_line))
plot!(x_line', [density_min';density_max'], color=:black)

```

---

<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:** [April 18, 2021, 4:53pm UTC](https://discourse.julialang.org/t/how-to-specify-the-span-of-hline-in-plots-jl/59539/9 "2021-04-18T16:53:44Z")

</div>

As a side note, the syntax of your last line works with `pyplot()` back-end but does not produce the expected results with the `gr()` back-end for instance. The following works for both:

```julia
plot!([x_line'; x_line'], [density_min'; density_max'], lc=:black)

```
