Bar plot with offset array

Hi everyone,

I want to plot a bar chart an array that starts from index 0. when I use plot() it works fine but when I use bar() it gives this error.
How can I plot the bar chart?

Thanks for any help!

using JuMP, StatsPlots;

bar(JuMP.value.(S))

ArgumentError: offset arrays are not supported but got an array with index other than 1

Stacktrace:
 [1] require_one_based_indexing at .\abstractarray.jl:89 [inlined]
 [2] diff(::OffsetArrays.OffsetArray{Int64,1,Array{Int64,1}}; dims::Int64) at .\multidimensional.jl:826
 [3] diff(::OffsetArrays.OffsetArray{Int64,1,Array{Int64,1}}) at .\multidimensional.jl:793
 [4] macro expansion at C:\Users\Morteza\.julia\packages\Plots\JKY3H\src\recipes.jl:377 [inlined]
 [5] apply_recipe(::RecipesPipeline.DefaultsDict, ::Type{Val{:bar}}, ::OffsetArrays.IdOffsetRange{Int64,Base.OneTo{Int64}}, ::OffsetArrays.OffsetArray{Float64,1,Array{Float64,1}}, ::Nothing) at C:\Users\Morteza\.julia\packages\RecipesBase\jcXIg\src\RecipesBase.jl:281
 [6] _process_seriesrecipe(::Plots.Plot{Plots.GRBackend}, ::RecipesPipeline.DefaultsDict) at C:\Users\Morteza\.julia\packages\RecipesPipeline\5b2IP\src\series_recipe.jl:48
 [7] _process_seriesrecipes!(::Plots.Plot{Plots.GRBackend}, ::Array{Dict{Symbol,Any},1}) at C:\Users\Morteza\.julia\packages\RecipesPipeline\5b2IP\src\series_recipe.jl:25
 [8] recipe_pipeline!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{JuMP.Containers.DenseAxisArray{Float64,2,Tuple{UnitRange{Int64},Base.OneTo{Int64}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}}}) at C:\Users\Morteza\.julia\packages\RecipesPipeline\5b2IP\src\RecipesPipeline.jl:95
 [9] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{JuMP.Containers.DenseAxisArray{Float64,2,Tuple{UnitRange{Int64},Base.OneTo{Int64}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}}}) at C:\Users\Morteza\.julia\packages\Plots\JKY3H\src\plot.jl:167
 [10] plot(::JuMP.Containers.DenseAxisArray{Float64,2,Tuple{UnitRange{Int64},Base.OneTo{Int64}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}}; kw::Base.Iterators.Pairs{Symbol,Symbol,Tuple{Symbol},NamedTuple{(:seriestype,),Tuple{Symbol}}}) at C:\Users\Morteza\.julia\packages\Plots\JKY3H\src\plot.jl:57
 [11] #bar#350 at C:\Users\Morteza\.julia\packages\RecipesBase\jcXIg\src\RecipesBase.jl:402 [inlined]
 [12] bar(::JuMP.Containers.DenseAxisArray{Float64,2,Tuple{UnitRange{Int64},Base.OneTo{Int64}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}}) at C:\Users\Morteza\.julia\packages\RecipesBase\jcXIg\src\RecipesBase.jl:402
 [13] top-level scope at In[469]:1 

Use parent(array) instead of array, this will strip the axis information and return the underlying array

But that defeats the purpose of the OffsetArray… This looks like a bug to me, bar(A) (with A an OffsetArray) should use the non-standard indices for the labels on the x axis, like plot(A) and scatter(A) do. It probably just hasn’t been implemented yet… A good starting point would be to file an issue!

Could you please explain more? I still cannot plot the bar chat.
Thank you.

I’m not sure which plotting package you’re using. In PyPlot, you need to specify bar(x,y). Given an OffsetVector. you may use bar(axes(v,1), parent(v)) to generate the plot.

Thanks for the reply,
but now it has a problem accessing the last index!

using JuMP, StatsPlots;

bar(axes(JuMP.value.(S),1), parent(JuMP.value.(S)))

BoundsError: attempt to access 52-element Array{Float64,1} at index [53]

Stacktrace:
 [1] getindex at .\array.jl:788 [inlined]
 [2] getindex at C:\Users\Morteza\.julia\packages\OffsetArrays\JNUm0\src\OffsetArrays.jl:153 [inlined]
 [3] macro expansion at C:\Users\Morteza\.julia\packages\Plots\JKY3H\src\recipes.jl:397 [inlined]
 [4] apply_recipe(::RecipesPipeline.DefaultsDict, ::Type{Val{:bar}}, ::UnitRange{Int64}, ::OffsetArrays.OffsetArray{Float64,1,Array{Float64,1}}, ::Nothing) at C:\Users\Morteza\.julia\packages\RecipesBase\jcXIg\src\RecipesBase.jl:281
 [5] _process_seriesrecipe(::Plots.Plot{Plots.GRBackend}, ::RecipesPipeline.DefaultsDict) at C:\Users\Morteza\.julia\packages\RecipesPipeline\5b2IP\src\series_recipe.jl:48
 [6] _process_seriesrecipes!(::Plots.Plot{Plots.GRBackend}, ::Array{Dict{Symbol,Any},1}) at C:\Users\Morteza\.julia\packages\RecipesPipeline\5b2IP\src\series_recipe.jl:25
 [7] recipe_pipeline!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{UnitRange{Int64},JuMP.Containers.DenseAxisArray{Float64,2,Tuple{UnitRange{Int64},Base.OneTo{Int64}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}}}) at C:\Users\Morteza\.julia\packages\RecipesPipeline\5b2IP\src\RecipesPipeline.jl:95
 [8] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{UnitRange{Int64},JuMP.Containers.DenseAxisArray{Float64,2,Tuple{UnitRange{Int64},Base.OneTo{Int64}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}}}) at C:\Users\Morteza\.julia\packages\Plots\JKY3H\src\plot.jl:167
 [9] plot(::UnitRange{Int64}, ::Vararg{Any,N} where N; kw::Base.Iterators.Pairs{Symbol,Symbol,Tuple{Symbol},NamedTuple{(:seriestype,),Tuple{Symbol}}}) at C:\Users\Morteza\.julia\packages\Plots\JKY3H\src\plot.jl:57
 [10] bar(::UnitRange{Int64}, ::Vararg{Any,N} where N; kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\Morteza\.julia\packages\RecipesBase\jcXIg\src\RecipesBase.jl:402
 [11] bar(::UnitRange{Int64}, ::Vararg{Any,N} where N) at C:\Users\Morteza\.julia\packages\RecipesBase\jcXIg\src\RecipesBase.jl:402
 [12] top-level scope at In[1118]:1