# Drawing a median line in a histogram in Plots

**URL:** https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-plots/63682
**Category:** General Usage
**Tags:** package, plotting
**Created:** [June 28, 2021, 9:29am UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-plots/63682 "2021-06-28T09:29:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)
#### Post date: [June 28, 2021, 9:29am UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-plots/63682/1 "2021-06-28T09:29:07Z")

</div>

Following the idea of the [link](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359), 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:

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

```

how to make the following lines of code run faster:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/2/e/2e8978c3f6e4c3b90f1d59d5d8c4df7d06184ef9.jpeg)

**full code**

```julia
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)

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [June 28, 2021, 12:40pm UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-plots/63682/2 "2021-06-28T12:40:53Z")

</div>

> [@HerAdri](#):
>
> It is possible to replace the following lines of code with a command:
> 
> ```julia
> plot(data,t=:histogram,normalize=true)
> subplot = plot!()[1]
> h = extrema(Plots.axis_limits(subplot, :y))[2]
> 
> ```

Possibly that might be written more simply as:

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

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [June 28, 2021, 12:58pm UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-plots/63682/3 "2021-06-28T12:58:19Z")

</div>

> [@HerAdri](#):
>
> how to make the following lines of code run faster:
> 
> ```julia
> plot!(x->pdf(dist , x), xlim=xlims(),color=:red)
> 
> ```

This seems faster:

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

```

---

<div class="post-metadata">

### Author: ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)
#### Post date: [June 28, 2021, 2:40pm UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-plots/63682/4 "2021-06-28T14:40:58Z")

</div>

h=max(ylims()…)  
👍

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [June 28, 2021, 2:46pm UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-plots/63682/5 "2021-06-28T14:46:57Z")

</div>

> [@HerAdri](#):
>
> how to avoid that, in different runs, the density plot overlaps the annotation “median = …”?

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