How to get current x and y limits of a plot? (Plots.jl)

Given that I have already made a plot using, say pl = plot([1,2,3]), is there a way to query what the current x and y limits of the plot are?

E.g. something like pl.xlims which would return (1.0,3.0).

I often find myself wanting to plot something abstract along with actual data, and this would be helpful for doing that while keeping the graph nice-looking. For example, I may be performing a numerical experiment and I want to plot step size vs error. To check the order convergence, I also want to plot lines of slopes 1, 2, 3, … (in log10 scale). But if I just do something like plot!(step_sizes, step_sizes .^ 4), the line extends way past my data, making the window zoom out a lot more. If I knew the xlims and ylims when plotting, I could prevent this.

Alternatively, if there is some sort of option to plot new data without adjusting the plot limits, e.g. pl = plot([1,2,3]); plot!(pl, [1,2,3,4,5,6], adjust_lims=false), and the second half of the second series just wouldn’t be visible in the window, that would also solve my problem.

It turns out you can do it using xlims(pl).

On a related note, how am I supposed to discover functionality in Plots.jl? For example, I don’t know where to find a list of all the keywords I can supply to the plot function.

Either check the attributes section and following of the documentation or call plotattr() in the REPL and explore via fuzzy finding.

2 Likes

Thanks!