How to set axes ranges based on one series but not another in Plots Recipe?

question

#1

With the following code, the y-axis is based on the distribution and adding the vertical line does not change that.

using Distributions, StatPlots
gr()

dist = Normal(1,1)
p = plot(dist, fill=(0,0.5))
plot!(p, [mean(dist)], linetype=:vline, label="")

I would like to achieve the same behavior within a Plots Recipe but if I add a vertical line as a new series then it changes the y-axis range.

struct SomeWrapper{T<:Distributions.Distribution}
    dist::T
end

using RecipesBase

@recipe function f(foo::SomeWrapper)
    @series begin
      fill --> (0, 0.5)
      foo.dist
    end
    @series begin
      linetype --> :vline
      label --> ""
      [mean(foo.dist)]
    end
end

plot(SomeWrapper(Normal(1,1)))

How can I fix that (i.e., force the ylims to be based only on the first series)?


#2

I don’t think you can do that in this case, the problem is that the Distribution is interpreted as a function and, so the max value is calculated later in this case. You may have to set ylim manually.


#3

Thanks for the quick reply!
I wonder if it would be worth it to add to recipes the ability to refer to plot attributes that will be calculated later on. The ability to access, for example, ylims() after the first series would easily solve this case as well as make it easy to position annotations etc.

By the way, do you consider this a bug? I would expect the vline to behave the same way when added with plot!() or inside a recipe…


#4

What are your thoughts on this, @mkborregaard?


#5

I consider it a bug, but a hard one to fix.