# Plotting Continuous Error Bars

**URL:** https://discourse.julialang.org/t/plotting-continuous-error-bars/70587
**Category:** New to Julia
**Tags:** plotting
**Created:** [October 29, 2021, 1:06am UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587 "2021-10-29T01:06:32Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)
#### Post date: [October 29, 2021, 1:06am UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/1 "2021-10-29T01:06:32Z")

</div>

Hello Everyone,

Are there any libraries out there that help continuous error plots with a small  
series of values.

For example,

x = [1,4,7,3,5,6,10,7]  
y = [1,2,3,4,5,6,7,8]

Using the _coefficient of variance_, or some other error variety,  
how might I approach plotting this series with high/low extrema?

Thank you,

---

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [October 29, 2021, 2:20am UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/2 "2021-10-29T02:20:25Z")

</div>

It sounds like you want to add ribbons to your plots:

> [@How to plot mean +/- deviation in Plots.jl?](https://discourse.julialang.org/t/how-to-plot-mean-deviation-in-plots-jl/1975/5):
>
> For future reference, this is the solution: 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.

> <https://stackoverflow.com/questions/48334036/how-to-fill-area-between-curves-with-plots-jl>

---

<div class="post-metadata">

### Author: ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)
#### Post date: [October 29, 2021, 6:17pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/3 "2021-10-29T18:17:02Z")

</div>

Hi There.

Thank you for sharing the example. It is about 3-years old  
and I was wondering if you could point me to a more current  
example?

If not, could you help me adapt it to a DF I am working with?

This reference code:

```julia
x = collect(range(0, 2, length= 100))
y1 = exp.(x)
y2 = exp.(1.3 .* x)
mid = (y1 .+ y2) ./ 2 #the midpoints (usually representing mean values)
w = (y2 .- y1) ./ 2 #the vertical deviation around the means
	
stp.plot(x, y1, fillrange = y2, fillalpha = 0.35, c = 1, label = "Confidence band", legend = :topleft)
stp.plot(x, mid, ribbon = w , fillalpha = 0.35, c = 1, lw = 2, legend = :topleft, label = "Mean")
stp.plot!(x,y1, line = :scatter, msw = 0, ms = 2.5, label = "Lower bound")
stp.plot!(x,y2, line = :scatter, msw = 0, ms = 2.5, label = "Upper bound")

```

The type of DF I am working with is:

```julia
Y = rand(1:5:300,10)
Col1 = rand(1:01:50,10)
Col2 = rand(1:02:75,10)

```

How do I assign a range in this example?

Is it necessary to use the (.exp) prefix? Instead  
should I use GLM to generate the coeffcients &  
intercept? If so, how might I apply to our use case?

Thank you ,

---

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [October 29, 2021, 9:39pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/4 "2021-10-29T21:39:29Z")

</div>

So, based on the example you are giving. If Y is the mean, Col1 is the lower bound and Col2 is the upper bound around the mean, you could do the following:

```julia
julia> using StatsPlots, DataFrames

julia> DF = DataFrame(mean = rand(1:5:300,10), lower_bound = rand(1:01:50,10), upper_bound = rand(1:02:75,10))
10×3 DataFrame
 Row │ mean lower_bound upper_bound
     │ Int64 Int64 Int64
─────┼─────────────────────────────────
   1 │ 46 6 23
   2 │ 201 43 31
   3 │ 36 48 37
   4 │ 51 12 17
   5 │ 291 42 45
   6 │ 16 42 75
   7 │ 81 3 65
   8 │ 221 50 35
   9 │ 81 6 73
  10 │ 241 24 59

julia> @df DF plot(:mean, ribbon=(:lower_bound,:upper_bound))

```

 ![sample_ribbon_plot](https://global.discourse-cdn.com/julialang/original/3X/9/5/951a16db1128601bf2d7258a675e1cd3c758688b.png)

---

<div class="post-metadata">

### Author: ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)
#### Post date: [October 30, 2021, 12:28pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/5 "2021-10-30T12:28:01Z")

</div>

Thank you

I am using StatsPlots with a PlotlyJS() backend  
attempting to display BOTH the upper and lower  
bounds when one hovers over the display, but  
the lower limit is only showing the axis label,  
not the lower bound. It is written as:

```julia
@df ceb stp.plot(:mean, ribbon=(:lower_bound, :upper_bound))
	stp.plot!(color=:blue, legend=:false,
		        title="Continuous Error Bar Plot",
		        background=:grey,
		        xticks=([1:1:10;],[1,2,3,4,5,6,7,8,9,10]), 
		        xlabel="Categories",
		        ylabel="Number")

```

Additionally, could you point me to a resource that  
explains how to change the ribbon color and series  
(line) color? So far I found [THIS](https://docs.juliaplots.org/latest/generated/plotlyjs/) but not sure it has  
information that will help this use case.

---

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [October 30, 2021, 1:14pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/6 "2021-10-30T13:14:39Z")

</div>

You can specify `color` for the line color and `fillcolor` for the ribbon color (you can do all of this in a single `plot` command instead of using two plot commands). You can find a full list of attributes here: [Series Attributes · Plots](https://docs.juliaplots.org/latest/generated/attributes_series/)

I’m not sure about the hover display on plotly. You might have to plot the ribbon boundary lines (mean + upper\_bound, and mean - lower\_bound. you can create additional columns in your DataFrame to store these and use them in additional `@dt ceb plot!` calls)

---

<div class="post-metadata">

### Author: ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)
#### Post date: [October 30, 2021, 2:35pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/7 "2021-10-30T14:35:41Z")

</div>

Hello

I was able to follow your instructions, but  
perhaps the StatsPlots feature with the  
PlotlyJS backend (not plotly, unless they  
are the same) does not allow the lower  
bounds to display.

Could you implement a MWE regarding  
“” plot the ribbon boundary lines (mean + upper\_bound, and mean - lower\_bound. you can create additional columns in your DataFrame to store these and use them in additional `@dt ceb plot!` calls)“”

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [October 30, 2021, 4:41pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/8 "2021-10-30T16:41:55Z")

</div>

Here is a VegaLite.jl example for this: [Error Bars & Error Bands · VegaLite.jl](https://www.queryverse.org/VegaLite.jl/stable/examples/examples_error_bars_bands/#Line-Chart-with-Confidence-Interval-Band-1)

---

<div class="post-metadata">

### Author: ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)
#### Post date: [October 30, 2021, 4:49pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/9 "2021-10-30T16:49:07Z")

</div>

@davidanthoff Thank you for this.

Would you be able to point me to the  
“cars” sata set they used in their case?

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [October 30, 2021, 8:26pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/10 "2021-10-30T20:26:48Z")

</div>

> [@YummyPampers2](#):
>
> Would you be able to point me to the  
> “cars” sata set they used in their case?

It is available via the VegaDatasets.jl package.

---

<div class="post-metadata">

### Author: ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)
#### Post date: [October 30, 2021, 9:15pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/11 "2021-10-30T21:15:44Z")

</div>

Hi,

So when in ran `plotly()` right before actually plotting, I did see that when you hover, you don’t see the value on the lower bound. However, you can change the behavior on the plotly display so that you can see either the upper bound, mean, or lower bound depending on which line you are close to. That way you don’t need to plot additional lines. I’ve highlighted the option in the top right corner of the plotly display below:

 ![PLOTLY](https://global.discourse-cdn.com/julialang/original/3X/b/0/b093210ef8db8826ec0ae5526ee566a79c26f867.png)

---

<div class="post-metadata">

### Author: ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)
#### Post date: [November 1, 2021, 3:42pm UTC](https://discourse.julialang.org/t/plotting-continuous-error-bars/70587/12 "2021-11-01T15:42:23Z")

</div>

@hdavid16

Thank you for this – glad you caught it,  
was getting ready to re-do the entire  
block.

Best,
