# How to plot mean +/- deviation in Plots.jl?

**URL:** https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975
**Category:** Visualization
**Tags:** question, plotting
**Created:** [February 8, 2017, 7:08am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975 "2017-02-08T07:08:32Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [February 8, 2017, 7:08am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/1 "2017-02-08T07:08:32Z")

</div>

Consider the example below:

```julia
using Plots

xs = linspace(1,100)
μs = log.(xs)
σs = rand(length(xs))

plot(xs,μs,grid=false,yerror=σs)

```

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

Is there any way to replace the error bars by a shaded transparent area with the same color as the mean or lighter?

Something like what we usually have in Gaussian Processes illustrations, but relating the color of the shaded area to the mean:

 ![GP](https://global.discourse-cdn.com/julialang/original/3X/e/4/e4ff136a8f67def210837cc3e5ceafe05c82d1f8.png)

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [February 8, 2017, 7:12am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/2 "2017-02-08T07:12:37Z")

</div>

`:ribbon`  
For some functions you could also check `groupapply` in StatPlots

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [February 8, 2017, 7:14am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/3 "2017-02-08T07:14:08Z")

</div>

To get the ribbon in a specific color but ‘lighter’, use the same color but set `fillalpha` to less than 1.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [February 8, 2017, 7:15am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/4 "2017-02-08T07:15:16Z")

</div>

Thanks @mkborregaard, where do I pass `:ribbon` in the plot command?

Can you share a working example based on the one I started?

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [February 8, 2017, 7:20am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/5 "2017-02-08T07:20:23Z")

</div>

For future reference, this is the solution:

```julia
using Plots

xs = linspace(1,100)
μs = log.(xs)
σs = rand(length(xs))

plot(xs,μs,grid=false,ribbon=σs,fillalpha=.5)

```

Thank you @mkborregaard.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [August 1, 2018, 7:22pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/6 "2018-08-01T19:22:14Z")

</div>

Is it possible to plot an “error” that asymmetric around the “mean”? That is, I want to plot the ribbon around the line `f(x)` with the error interval `[f(x) - err1(x), f(x) + err2(x)]`, where `err1(x) != err2(x)`.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [August 6, 2018, 11:04pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/7 "2018-08-06T23:04:18Z")

</div>

`ribbon = (err1(x), err2(x))`

---

<div class="post-metadata">

### Author: ![Andre\_Mello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andre_mello/32/7333_2.png) [@Andre\_Mello](https://discourse.julialang.org/u/Andre_Mello)
#### Post date: [January 27, 2020, 5:05pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/8 "2020-01-27T17:05:48Z")

</div>

Sorry for reviving this thread, but I believe this question falls nicely into this topic.

I can’t seem to be able to display asymmetric error bars in scatter plots. Is there a way?  
Something along these lines:

```julia
a = 1:10
b = randn(10)
med_b = median(b)
scatter((a, med_b),
		yerror=(med_b-minimum(b),
				maximum(b)-med_b))

```

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [January 27, 2020, 6:11pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/9 "2020-01-27T18:11:26Z")

</div>

That should more or less work but `med_b`, `minimum(b)` etc are just scalars. `a` is a vector. Are you sure this is what you want?

---

<div class="post-metadata">

### Author: ![Andre\_Mello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andre_mello/32/7333_2.png) [@Andre\_Mello](https://discourse.julialang.org/u/Andre_Mello)
#### Post date: [January 27, 2020, 6:39pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/10 "2020-01-27T18:39:06Z")

</div>

Sorry @mkborregaard , I made some last minute changes to the MWE and broke it. It should just be:

```julia
a = 0.0
b = randn(10)
med_b = median(b)
max_b = maximum(b)
min_b = minimum(b)
scatter((a, med_b),
		yerror=(med_b-min_b,
				max_b-med_b))

```

Which gives me

```julia
unexpected ebi type Array{Tuple{Float64,Float64},0} for errorbar: (1.6075403984383283, 1.2973639900098863)

```

However, since you mentioned it, I decided trying this out with vectors and it indeed works.

```julia
a = 1:10
b = [randn(10) for _ in a]
med_b = [median(x) for x in b]
max_b = [maximum(x) for x in b]
min_b = [minimum(x) for x in b]
scatter((a, med_b),
		yerror=(med_b-min_b,
				max_b-med_b))

```

This gives me what I want.

But now I’m not sure what I’m missing in the first MWE.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [January 27, 2020, 8:51pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/11 "2020-01-27T20:51:48Z")

</div>

You’re not supposed to plot scalars, I think that’s it

---

<div class="post-metadata">

### Author: ![Andre\_Mello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andre_mello/32/7333_2.png) [@Andre\_Mello](https://discourse.julialang.org/u/Andre_Mello)
#### Post date: [January 28, 2020, 11:04am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/12 "2020-01-28T11:04:29Z")

</div>

Yeah, that’s what I thought for a while as well.

Until I decided to give this a shot

```julia
a = 0.0
b = randn(10)
med_b = median(b)
max_b = maximum(b)
min_b = minimum(b)
scatter((a, med_b),
		yerror=[(med_b-min_b,
				max_b-med_b)])

```

and it works. Kind of unintuitive at first, having to wrap a tuple into an array, given that when working with arrays it’s the other way around.

Either way, problem solved.

---

<div class="post-metadata">

### Author: ![diadora77](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diadora77/32/220469_2.png) [@diadora77](https://discourse.julialang.org/u/diadora77)
#### Post date: [March 9, 2021, 1:49am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/13 "2021-03-09T01:49:47Z")

</div>

Building on this example, is there a way to ensure that the label for the plot uses the marker “shape” and not the “ribbon” color? Use case is when printed (black and white), it’s still clear which label goes with which line. Example below, label is tied to ribbon color, not marker shape.

pyplot()  
xs = range(1,20,step=1)  
μs1 = log.(xs)  
σs1 = rand(length(xs))  
μs2 = log10.(xs)  
σs2 = rand(length(xs))

Plots.plot( xs,μs1,grid=false,ribbon=σs1, shape=:circle, label=“μs1”)  
Plots.plot!(xs,μs2,grid=false,ribbon=σs2, shape=:square, label=“μs2”)

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 9, 2021, 7:47am UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/14 "2021-03-09T07:47:36Z")

</div>

Could you show what the problem looks like? Running your code with the GR backend (I don’t have pyplot), I get:

![image](https://global.discourse-cdn.com/julialang/original/3X/5/7/57b44b8afa0650856b62ad4a41bfe03f4db5ad43.png)

which looks to me like the legend uses the marker shape? If you’re seeing something different it might be a backend-specific issue.

---

<div class="post-metadata">

### Author: ![diadora77](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diadora77/32/220469_2.png) [@diadora77](https://discourse.julialang.org/u/diadora77)
#### Post date: [March 9, 2021, 12:05pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/15 "2021-03-09T12:05:02Z")

</div>

Yes, it must be backend. Here’s what it looks like with pyplot: ![fig_ribbon_label](https://global.discourse-cdn.com/julialang/original/3X/6/6/662f874bf0dfb446319a659530f5af803552cdaa.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: [March 10, 2021, 7:41pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/16 "2021-03-10T19:41:12Z")

</div>

This workaround seems to work in Julia PyPlot:

```julia
using Plots; pyplot()
xs = range(1,20,step=1)
μs1, σs1 = log.(xs), rand(length(xs))
μs2, σs2 = log10.(xs), rand(length(xs))
Plots.plot( xs,μs1, color=:lightblue, ribbon=σs1,label=false)
Plots.plot!(xs,μs2, color=:pink, ribbon=σs2,label=false)
Plots.plot!(xs,μs1, color=:blue, marker=(:circle, 8, 1.), label="μs1")
Plots.plot!(xs,μs2, color=:red, marker=(:square, 8, 1.), label="μs2")

```

 ![PyPlot_markers_legend](https://global.discourse-cdn.com/julialang/original/3X/8/6/86792d1eaf8f9b85a8cc47e4bb086c6e5d5142f2.png)

---

<div class="post-metadata">

### Author: ![Dario\_Sarra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dario_sarra/32/9338_2.png) [@Dario\_Sarra](https://discourse.julialang.org/u/Dario_Sarra)
#### Post date: [April 29, 2021, 4:59pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/17 "2021-04-29T16:59:50Z")

</div>

I don’t manage to plot asymmetric error using ribbon. Is this an expected outcome or a bug?

---

<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: [April 29, 2021, 5:18pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/18 "2021-04-29T17:18:27Z")

</div>

The examples provided [here](https://stackoverflow.com/a/37704547/14071775) do work fine for both Plots.jl gr() and pyplot() backends:

```julia
Plots.plot( xs,μs1, color=:lightblue, ribbon=(σs1,σs1/3),label=false)

```

![asymmetric_gr_ribbon](https://global.discourse-cdn.com/julialang/original/3X/2/c/2ceba0a22c7ee5b344f05f3aad6b3a8073398364.png)

---

<div class="post-metadata">

### Author: ![Dario\_Sarra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dario_sarra/32/9338_2.png) [@Dario\_Sarra](https://discourse.julialang.org/u/Dario_Sarra)
#### Post date: [April 30, 2021, 1:40pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/19 "2021-04-30T13:40:24Z")

</div>

Possibly the issue arises using StatsPlots @df macro. I have all my errors in one dataframe column where each entry is a tuple. I guess this is why doesn’t work in general. However it seems to be fine with Plotlyjs

---

<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: [April 30, 2021, 1:43pm UTC](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/20 "2021-04-30T13:43:44Z")

</div>

@Dario_Sarra, please provide a MWE.

[Next page](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975.md?page=2)
