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
vline!(, color=:darkred, label="1")
vline!(, color=:black, label="2")
hspan!, but to no avail.
You know both
y coordinates at both ends, so you can
plot([x, x], [ymin, ymax])
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)
Thank you guys. I don’t know why I didn’t think of such a simple solution
What’s wrong with
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,
hspan are specifically made for spanning the entire plot range vertically/hoirzontally
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
# add density lines
x = -3:.05:3
y = pdf.(Normal(0,1), x)
x_line = [.6,1,1.8]
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)