# Asymmetric error bars and box plots in Julia

**URL:** <https://discourse.julialang.org/t/asymmetric-error-bars-and-box-plots-in-julia/73647>\
**Category:** New to Julia\
**Tags:** plotting, statsplots\
**Created:** [December 26, 2021, 3:26pm UTC](https://discourse.julialang.org/t/asymmetric-error-bars-and-box-plots-in-julia/73647 "2021-12-26T15:26:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newtothis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/newtothis/32/32227_2.png) [@newtothis](https://discourse.julialang.org/u/newtothis)\
**Post date:** [December 26, 2021, 3:26pm UTC](https://discourse.julialang.org/t/asymmetric-error-bars-and-box-plots-in-julia/73647/1 "2021-12-26T15:26:51Z")

</div>

I have a Julia program that gives me data in the following form. For each point on X, I get a vector for the Y axis. I want to plot a boxplot for each X value using the vector of Y values. So I want the median to be the center of the boxplot and the error bars to basically be the first and the third quartile. Is there some inbuilt function in Julia which can help achieve this? If not, how do I go about this?

---

<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:** [December 26, 2021, 5:48pm UTC](https://discourse.julialang.org/t/asymmetric-error-bars-and-box-plots-in-julia/73647/2 "2021-12-26T17:48:01Z")

</div>

That is what ` StatsPlots`’s ` boxplot()` does.  
From your description, the data may need some pre-conditioning, for example:

```julia
using StatsPlots, DataFrames
X = 0:2.5:10
n = length(X)
Y = [rand(-3:3, 10) for _ in 1:n]
X2 = [fill(x,length(y)) for (x,y) in zip(X,Y)]
df = DataFrame(X = X2, Y = Y)
@df df boxplot(:X, :Y, legend=false)

```

![StatsPlot_Boxplot](https://global.discourse-cdn.com/julialang/original/3X/e/2/e2e09b9d6b31d915e2a13bd75e4e651fc014694d.png)

---

<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:** [December 27, 2021, 8:43pm UTC](https://discourse.julialang.org/t/asymmetric-error-bars-and-box-plots-in-julia/73647/3 "2021-12-27T20:43:59Z")

</div>

In case you would want to plot only the error bars around the median for the first and third quartiles, in the example above, you could do:

```julia
using Statistics
Ym = median.(Y)
ϵ⁻ = Ym .- quantile.(Y, fill(0.25, n))
ϵ⁺ = quantile.(Y, fill(0.75, n)) .- Ym
scatter(X, Ym, ms=6, yerror=(ϵ⁻, ϵ⁺), legend=false)

```

![StatsPlot_quantile_error_bars](https://global.discourse-cdn.com/julialang/original/3X/6/6/66b050034d19a617ee15048cd8bf09dd558369d9.png)

---

<div class="post-metadata">

**Author:** ![newtothis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/newtothis/32/32227_2.png) [@newtothis](https://discourse.julialang.org/u/newtothis)\
**Post date:** [January 22, 2022, 6:56pm UTC](https://discourse.julialang.org/t/asymmetric-error-bars-and-box-plots-in-julia/73647/4 "2022-01-22T18:56:41Z")

</div>

@rafael.guerra Thank you for the answer, and please accept my apologies for the delay in my response. I was able to figure out the general boxplot on my own, but I didn’t get back to the forum soon enough. However, your other answer on displaying only the error bars is also very helpful. I’m likely going to end up using both.

Thanks!
