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

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

``````using Plots

pyplot()

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

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

Thank you.

1 Like

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

1 Like

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

``````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)
`````` 1 Like

Thank you guys. I don’t know why I didn’t think of such a simple solution What’s wrong with `hspan!`?

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

julia> hspan!([0.1, 0.5])
`````` 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?

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

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.

``````using Plots, Distributions
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)
``````

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:

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