# Plotting bar chart inside a loop

**URL:** <https://discourse.julialang.org/t/plotting-bar-chart-inside-a-loop/11646>\
**Category:** General Usage\
**Created:** [June 13, 2018, 1:56pm UTC](https://discourse.julialang.org/t/plotting-bar-chart-inside-a-loop/11646 "2018-06-13T13:56:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [June 13, 2018, 1:56pm UTC](https://discourse.julialang.org/t/plotting-bar-chart-inside-a-loop/11646/1 "2018-06-13T13:56:15Z")

</div>

I would like to plot a bar chart like this following one :  
 ![Sans%20titre](https://global.discourse-cdn.com/julialang/original/3X/f/0/f0fc9eb9f2abf8e74fae1361f3b3e3248079d98e.png)

The x-axe data are represented by dates

```julia
x_years = ["2018", "2019"]

```

and my y-axe data represents the amount of production of a given product. This information comes from a dictionary I have created.

```julia
amount_product[productA,NY] = [10, 15]

```

That means that were produced 10 products A in 2018 and 15 in 2019, in NY region.  
I have tried the following code to try to build this:

```julia
plt= []
for product in list_product   
    for r in list_regions
            plt = bar!(x_years, amount_product[product,r])
        end
    end
end
display(plt)

```

I got the following result  
 ![newplot](https://global.discourse-cdn.com/julialang/original/3X/b/8/b8805db44277be674dc07e5f78b2eef42bfff6ae.png)  
I know it’s like plt only keep the last value from the dictionary but I don’t know how to make it differently. Anyone has an idea to help me build this kind of bar chart? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [June 13, 2018, 3:38pm UTC](https://discourse.julialang.org/t/plotting-bar-chart-inside-a-loop/11646/2 "2018-06-13T15:38:09Z")

</div>

Use `groupedbar` (once you get StatPlots installed) [https://github.com/JuliaPlots/StatPlots.jl#grouped-bar-plots](https://github.com/JuliaPlots/StatPlots.jl#grouped-bar-plots)  
.

---

<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:** [June 15, 2018, 9:42pm UTC](https://discourse.julialang.org/t/plotting-bar-chart-inside-a-loop/11646/3 "2018-06-15T21:42:07Z")

</div>

With [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) you can do it this way:

```julia
using VegaLite, DataFrames

# Create a DataFrame in tidy format
df = DataFrame(
    product=["A", "A", "B", "B"],
    year=[2018, 2019, 2018, 2019],
    amount=[4., 5., 3, .6])

# Plot
df |> @vlplot(:bar, x="year:N", y=:amount, color=:product)

```

And the plot will look like this:  
 ![plot](https://global.discourse-cdn.com/julialang/original/3X/1/b/1bed09c80a15756cae04727788b977dfc4ddfcd2.png)
