# Drawing a median line in a histogram in Makie

**URL:** <https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [June 22, 2021, 11:59am UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359 "2021-06-22T11:59:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 22, 2021, 11:59am UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359/1 "2021-06-22T11:59:18Z")

</div>

I can’t figure out a non-manual way to do this:

```julia
mwe_fig = let
	fig = Figure()
	data = randn(1000) .+ 1
	ax = Axis(fig[1,1]; xlabel="Value", ylabel="Count")
	hist!(ax, data)
	m = median(data)
	m_color = Makie.wong_colors()[6]
	linesegments!(ax, [Point2f0(m, 0), Point2f0(m, 250)]; color=m_color, linewidth=2)
	#vlines!(ax, [m]; color=m_color, ymin=0.046, linewidth=2)
	text!(string("median: ", @sprintf("%.0f", m)), position = (m*1.1, 225), align = (:left, :center), color=m_color)
	fig
end

```

 ![image (6)](https://global.discourse-cdn.com/julialang/original/3X/7/4/74112ae1b00601744554f876dcee395b5b682b09.png)

I’d prefer if I didn’t have to guess the height and tweak it manually. `vlines!` seems appropriate, except the line starts below 0, which looks a bit odd to me:

 ![image (7)](https://global.discourse-cdn.com/julialang/original/3X/b/c/bc0492755994ea207d48c6fb46c30c6cc9c748e6.png)

and I still need to guess the height of the text. I can adjust `ymin` by hand (`ymin=0.046` seems to work pretty well) though.

I tried using `ax.limits` to get the height, but it’s `(nothing, nothing)`.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 22, 2021, 12:11pm UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359/2 "2021-06-22T12:11:39Z")

</div>

The actual limits are in `ax.finallimits`. I usually prefer to do `ylims!(low = 0)` anyway with barplots if they show only positive values, in which case `vlines` would be ok. If you plot a line manually and don’t want it to change the limits itself, you should be able to set `yautolimits = false` as an attribute.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 22, 2021, 12:38pm UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359/3 "2021-06-22T12:38:28Z")

</div>

`ylims!(low = 0) ` plus `ax.finallimits` for the height works well, thanks!

```julia
mwe_fig = let
	fig = Figure()
	data = randn(1000) .+ 1
	ax = Axis(fig[1,1]; xlabel="Value", ylabel="Count")
	hist!(ax, data)
	m = median(data)
	m_color = Makie.wong_colors()[6]
	vlines!(ax, [m]; color=m_color, linewidth=2)
	h = ax.finallimits[].widths[2]
	text!(string("median: ", @sprintf("%.0f", m)), position = (m*1.1, h), align = (:left, :center), color=m_color)
	ylims!(low=0)
	fig
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/d/1d40f4c9e44a2935d82d0f6cab650735d58e1ac6.png)

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 27, 2021, 10:40pm UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359/4 "2021-06-27T22:40:58Z")

</div>

This kind of thing also comes up when labelling points on a line. For example, if I have something like

```julia
let
	xs = range(1, 100; length=100)
	ys = 1 ./ xs
	fig = Figure()
	ax = Axis(fig[1,1])
	lines!(ax, xs, ys; linewidth=3)
	i = 5 # a point we wish to label
	scatter!(ax, [Point2f0(xs[i], ys[i])])
	text!(ax, @sprintf("%0.2f", ys[i]); position = (xs[i], ys[i]), align=(:left, :bottom))
	fig
end

```

it comes out as

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/4/4499c4797e766203e418bee626eb158440700215.png)

And to me it looks like the text is a little too close to the line. I can shift it over with say

```julia
text!(ax, @sprintf("%0.2f", ys[i]); position = (xs[i] + 1, ys[i]), align=(:left, :bottom))

```

which comes out fine:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/2/a26bc77eb7d93064ab4a9f9e71309ced547ba87c.png)

But if I’m making several plots with different axes (say in some the x-limits are (0,1) and in others (0, 100)) then I think you need the `finallimits` thing to get the right scaling to be like 1% of the x-range or such.

I think both of these are cases in which there’s a need to mix data scale and axis scale. I wonder if Makie could have like a `AxisScale` object that you could do `position = (xs[i] + .01*AxisScale(), ys[i])` or something? Or `position_in_axis_scale = (convert_to_axis_units(xs[i]) + .01, convext_to_axis_units(ys[i]))` or something like that (with better names)?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 28, 2021, 5:18am UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359/5 "2021-06-28T05:18:26Z")

</div>

For text there is `offset` which works in screen coordinates. In principle I agree that axis transforms are useful but your specific issue is easier to handle with the offset parameter:

Have a look at the last example here [http://makie.juliaplots.org/stable/plotting\_functions/text.html](http://makie.juliaplots.org/stable/plotting_functions/text.html)

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 28, 2021, 10:14am UTC](https://discourse.julialang.org/t/drawing-a-median-line-in-a-histogram-in-makie/63359/6 "2021-06-28T10:14:33Z")

</div>

Ah, thanks! I had missed that section (only saw the docstring). That looks great for this.
