Plots.jl error bar bar()

Hello everyone

I have a little problem. How to put the yerr in the bar chart? The code below does not work :frowning:

using Plots
plotlyjs()

bar_plot = bar()

data = [(rand()+1) * randn(n) + i for i in 1:5]

for i = 1:5
    bar_plot = bar!([mean(data[i])], yerr = std(data[i]))
end

bar_plot

Maybe this is backend specific? I know Plotly can’t do ribbon plots.

Hi Chris
I tried with the GR and the result is the same.

1 Like

You probably meant:

bar_plot = bar()

for i = 1:5
    bar!(bar_plot, [mean(data[i])], yerr = std(data[i]))
end

bar_plot

where the function bar! is modifying the plot in place (see the Plots.jl docs for this “in place” plotting).
For some reason though, when adding more bar plots, the error bars don’t move and all stay in the middle of the plot (at least with plotlyjs()), I guess you could open an issue for that on Plots.jl repo.

It’s probably easier and more readable to do it in one command:

bar(mean.(data), yerr = std.(data))

As a minor OT: there is the StatPlots.shadederror function in StatPlots.jl that can do ribbon like plots (i.e. solid line with semitransparent ribbon around it) on all backends I’ve tested (including plotly).

1 Like

Hi plever
When I run the code, it occurs:

WARNING: Array{T}(::Type{T}, m::Int) is deprecated, use Array{T}(m) instead.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] Array(::Type{Float64}, ::Int64) at ./deprecated.jl:57
 [3] error_coords(::UnitRange{Int64}, ::Array{Float64,1}, ::Float64) at /home/jmarcellopereira/.julia/v0.6/Plots/src/recipes.jl:706
 [4] macro expansion at /home/jmarcellopereira/.julia/v0.6/Plots/src/recipes.jl:730 [inlined]
 [5] apply_recipe(::Dict{Symbol,Any}, ::Type{Val{:yerror}}, ::UnitRange{Int64}, ::Array{Float64,1}, ::Void) at /home/jmarcellopereira/.julia/v0.6/RecipesBase/src/RecipesBase.jl:259
 [6] _process_seriesrecipe(::Plots.Plot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}) at /home/jmarcellopereira/.julia/v0.6/Plots/src/pipeline.jl:404
 [7] _plot!(::Plots.Plot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{Float64,1}}) at /home/jmarcellopereira/.julia/v0.6/Plots/src/plot.jl:227
 [8] #plot!#141(::Array{Any,1}, ::Function, ::Plots.Plot{Plots.PlotlyJSBackend}, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at /home/jmarcellopereira/.julia/v0.6/Plots/src/plot.jl:151
 [9] (::Plots.#kw##plot!)(::Array{Any,1}, ::Plots.#plot!, ::Plots.Plot{Plots.PlotlyJSBackend}, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at ./<missing>:0
 [10] #bar!#389(::Array{Any,1}, ::Function, ::Plots.Plot{Plots.PlotlyJSBackend}, ::Vararg{Any,N} where N) at /home/jmarcellopereira/.julia/v0.6/RecipesBase/src/RecipesBase.jl:355
 [11] (::Plots.#kw##bar!)(::Array{Any,1}, ::Plots.#bar!, ::Plots.Plot{Plots.PlotlyJSBackend}, ::Vararg{Any,N} where N) at ./<missing>:0
 [12] macro expansion at ./In[10]:4 [inlined]
 [13] anonymous at ./<missing>:?
 [14] include_string(::String, ::String) at ./loading.jl:515
 [15] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/jmarcellopereira/.julia/v0.6/IJulia/src/execute_request.jl:160
 [16] eventloop(::ZMQ.Socket) at /home/jmarcellopereira/.julia/v0.6/IJulia/src/eventloop.jl:8
 [17] (::IJulia.##11#14)() at ./task.jl:335
while loading In[10], in expression starting on line 3

It simply means that the Plots.jl package has not yet solved all the deprecation warnings relative to julia 0.6. It’s safe for you to ignore the warning I think.

As for your plot, as mentioned on gitter, you should maybe also specify the x coordinate, to be sure you know where the bars end up, i.e. :

for i = 1:5
    bar!(bar_plot, [i], [mean(data[i])], yerr = std(data[i]))
end