Drawing a median line in a histogram in Plots

Following the idea of the link, I reproduced the graph using the Plots package, and I got the following questions:
It is possible to replace the following lines of code with a command:

subplot = plot!()[1]
h = extrema(Plots.axis_limits(subplot, :y))[2]

how to make the following lines of code run faster:

plot!(x->pdf(dist , x), xlim=xlims(),color=:red)

how to avoid that, in different runs, the density plot overlaps the annotation “median = …”?
plots_median

full code

using Plots, Statistics, Distributions, Printf

dist = Normal(0, 1).+1

data = rand(dist, 1000)

plot(data,t=:histogram,normalize=true)

subplot = plot!()[1]

h = extrema(Plots.axis_limits(subplot, :y))[2]

m = median(data)

plot!([m],linetype=:vline,widths=3)

annotate!(m*1.1, h, Plots.text(string("median: ", @sprintf("%.0f", m)), :green, :left, 6))

#using Distributions

plot!(x->pdf(dist , x), xlim=xlims(),color=:red)

plot!(legend = false)
1 Like

Possibly that might be written more simply as:

plot(data,t=:histogram,normalize=true)
h = ylims()[2]

This seems faster:

x = LinRange(xlims()...,100)
plot!(x, pdf(dist , x), xlim=xlims(),color=:red)
1 Like

h=max(ylims()…)
:+1:

1 Like

As for your last question, the simplest is to display the annotation higher in the plot.