4

How can I access the attributes of a plot after creating it?

For example, I was hoping to get the y-axis limits from a plot. Something like this didn't work:

p1 = Plots.plot(rand(10))
get(p1,:ylim)

This errors:

MethodError: no method matching get(::Plots.Plot{Plots.GRBackend}, ::Symbol)
Alec
  • 4,235
  • 1
  • 34
  • 46

1 Answers1

8

Plots are organized into subplots (often just one) which are organized into series. In this case you want the y axis limits, which is a function of the subplot. Normally you can get a named attribute (e.g. y) from the first series in the first subplot by

p1[1][1][:y]

In the case of limits it's a little more complicated, they are properties of Axis which are owned by the subplot. But luckily you can just do

ylims(p1)
Michael K. Borregaard
  • 7,864
  • 1
  • 28
  • 35