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



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

using Distributions, StatPlots

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}

using RecipesBase

@recipe function f(foo::SomeWrapper)
    @series begin
      fill --> (0, 0.5)
    @series begin
      linetype --> :vline
      label --> ""


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


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.


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…


What are your thoughts on this, @mkborregaard?


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