# Bar plot with offset array

**URL:** <https://discourse.julialang.org/t/bar-plot-with-offset-array/41122>\
**Category:** Visualization\
**Tags:** question, plotting, jump, statsplots\
**Created:** [June 10, 2020, 11:34am UTC](https://discourse.julialang.org/t/bar-plot-with-offset-array/41122 "2020-06-10T11:34:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.lavaiyan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m.lavaiyan/32/15846_2.png) [@m.lavaiyan](https://discourse.julialang.org/u/m.lavaiyan)\
**Post date:** [June 10, 2020, 11:34am UTC](https://discourse.julialang.org/t/bar-plot-with-offset-array/41122/1 "2020-06-10T11:34:21Z")

</div>

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!

```julia
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 

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [June 10, 2020, 12:55pm UTC](https://discourse.julialang.org/t/bar-plot-with-offset-array/41122/2 "2020-06-10T12:55:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [June 10, 2020, 2:11pm UTC](https://discourse.julialang.org/t/bar-plot-with-offset-array/41122/3 "2020-06-10T14:11:21Z")

</div>

> [@jishnub](#):
>
> 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!

---

<div class="post-metadata">

**Author:** ![m.lavaiyan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m.lavaiyan/32/15846_2.png) [@m.lavaiyan](https://discourse.julialang.org/u/m.lavaiyan)\
**Post date:** [June 10, 2020, 9:31pm UTC](https://discourse.julialang.org/t/bar-plot-with-offset-array/41122/4 "2020-06-10T21:31:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [June 11, 2020, 4:05am UTC](https://discourse.julialang.org/t/bar-plot-with-offset-array/41122/5 "2020-06-11T04:05:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![m.lavaiyan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m.lavaiyan/32/15846_2.png) [@m.lavaiyan](https://discourse.julialang.org/u/m.lavaiyan)\
**Post date:** [June 11, 2020, 10:29am UTC](https://discourse.julialang.org/t/bar-plot-with-offset-array/41122/6 "2020-06-11T10:29:42Z")

</div>

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

```julia
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

```
