Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hline/vline with subplots in Julia

I'm trying to add a horizontal line to a subplot, and from this discussion: https://discourse.julialang.org/t/vline-with-subplots/25479/2, I have the following

x = [1,2,3]
y1 = 2x
y2 = x.^2
plot([x, x], [y1, y2], layout = (2, 1))
hline!([4 4])

Which produces the plots.

plots

Now what I'm trying to do is do the horizontal line on the bottom plot, but not the top one. If I just specify hline!([4]) , it defaults to the top one. Is there a way to do the bottom one only?

like image 678
Bebotron Avatar asked Dec 04 '25 13:12

Bebotron


1 Answers

The trick is to keep track of the plot handles.

p = plot([x, x], [y1, y2], layout = (2, 1))

returns a plot handle (specifically, a Plots.Plot{Plots.GRBackend} object) p with two elements, p[1] (the first subplot) and p[2] (the second subplot). To add the hline to the bottom plot only, then, you can write:

x = [1,2,3]
y1 = 2x
y2 = x.^2
p = plot([x, x], [y1, y2], layout = (2, 1))
hline!(p[2], [4])

enter image description here

like image 179
cbk Avatar answered Dec 07 '25 14:12

cbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!