Plotting Continuous Error Bars

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,

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

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:

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:

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 ,

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

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:

@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 but not sure it has
information that will help this use case.

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

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)

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)""

Here is a VegaLite.jl example for this: Error Bars & Error Bands Β· VegaLite.jl

@davidanthoff Thank you for this.

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.

1 Like

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:

@hdavid16

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

Best,

1 Like