xiaodai
xiaodai

Reputation: 16004

What's Julia's Plots.jl's equivalent of R's `abline`?

R's abline draws a straight line parameterised by y = ax+b on the x-y 2D coordinate plan.

What's Julia's equivalent of that in Plots.jl?

Upvotes: 4

Views: 1817

Answers (2)

Ken Williams
Ken Williams

Reputation: 23975

If the line you want to add isn't vertical, you can also just plot it as a functional expression:

plot(x->((x^2+3x+2)/(x-2)), 6, 30, xlim=(6,30), size=(300,300))
plot!(x->x+5, color="red")

That may adjust the bounds of the existing plot, unlike @hckr's abline! solution, which may or may not be what you want.

Upvotes: 1

hckr
hckr

Reputation: 5583

There is Plots.abline! which draws a straight line in the form of ax+b respecting axis limits of the plot. So it will not actually go to infinity but does not require you to know what the axis limits are beforehand.

plot(x->((x^2+3x+2)/(x-2)), 6, 30, xlim=(6,30), size=(300,300))
# draw oblique asymptote to the above function (y=x+5)
Plots.abline!(1, 5, line=:dash)

You can also plot a straight line using only two points that are on the line. This should also respect the axis limits.

plot!([x1, x2], [y1, y2], seriestype = :straightline, kwargs...)

Upvotes: 6

Related Questions