# Plots: bars side by side

**URL:** <https://discourse.julialang.org/t/plots-bars-side-by-side/6655>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [October 24, 2017, 1:36pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655 "2017-10-24T13:36:19Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaslusimoes](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kaslusimoes](https://discourse.julialang.org/u/kaslusimoes)\
**Post date:** [October 24, 2017, 1:36pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/1 "2017-10-24T13:36:19Z")

</div>

Hi there,

I’m using Plots.jl (with PyPlot as the backend) and trying to plot some bars side by side, as in the following example from matplotlib: [http://matplotlib.org/examples/api/barchart\_demo.html](http://matplotlib.org/examples/api/barchart_demo.html) (figure below)

![](https://global.discourse-cdn.com/julialang/original/3X/0/f/0fcbcd6a7b65a5726a5ba67a9801aeade592c7a3.png)

Looking at the documentation of Plots it seemed to me that the attribute I wanted might be `bar_position = :stack`, but it didn’t work out 😕 the bars are still overlapping.

Can anyone help with this plot?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 24, 2017, 1:43pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/2 "2017-10-24T13:43:42Z")

</div>

[https://github.com/JuliaPlots/StatPlots.jl#grouped-bar-plots](https://github.com/JuliaPlots/StatPlots.jl#grouped-bar-plots)

---

<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:** [October 24, 2017, 1:58pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/3 "2017-10-24T13:58:28Z")

</div>

```julia
mn = [20, 35, 30, 35, 27,25, 32, 34, 20, 25]
sx = repeat(["Men", "Women"], inner = 5)
std = [2, 3, 4, 1, 2, 3, 5, 2, 3, 3]
nam = repeat("G" .* string.(1:5), outer = 2)

using StatPlots
groupedbar(nam, mn, yerr = std, group = sx, ylabel = "Scores", 
        title = "Scores by group and gender")

```

![03](https://global.discourse-cdn.com/julialang/original/3X/9/0/9007b99be14687201ffc1f07df31aeb0b2e0f388.png)

---

<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:** [October 24, 2017, 2:07pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/4 "2017-10-24T14:07:29Z")

</div>

Or, to get something closer to your example, you can modify a little:

```julia
groupedbar(nam, mn, yerr = std, group = sx, ylabel = "Scores", 
        title = "Scores by group and gender", bar_width = 0.67, 
        lw = 0, c = [:red :darkkhaki], markerstrokewidth = 1.5,
        framestyle = :box, grid = false, yticks = 0:5:35)

```

![04](https://global.discourse-cdn.com/julialang/original/3X/1/2/126bb092b7b98d0ddfe2e5388e288f69b9df1c35.png)

---

<div class="post-metadata">

**Author:** ![kaslusimoes](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kaslusimoes](https://discourse.julialang.org/u/kaslusimoes)\
**Post date:** [October 24, 2017, 2:22pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/5 "2017-10-24T14:22:14Z")

</div>

Thanks very much @ChrisRackauckas and @mkborregaard. Now with mkborregaard’s examples I can make it the way I wanted

Somehow I missed the StatPlots package 😅

---

<div class="post-metadata">

**Author:** ![kaslusimoes](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kaslusimoes](https://discourse.julialang.org/u/kaslusimoes)\
**Post date:** [October 24, 2017, 2:29pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/6 "2017-10-24T14:29:17Z")

</div>

Maybe it’s a good idea to include this as an example to the README of StatPlots?

Just from looking at the one provided now on GitHub I wouldn’t be able to get my plot the way I wanted to

---

<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:** [October 24, 2017, 2:43pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/7 "2017-10-24T14:43:41Z")

</div>

You’re welcome to put a strong example together and submit a PR 🙂

---

<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:** [October 24, 2017, 3:10pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/8 "2017-10-24T15:10:15Z")

</div>

The syntax in the StatPlots readme uses matrix inputs - that’s also valid but less common:

```julia
men = [20, 35, 30, 35, 27]
women = [25, 32, 34, 20, 25]
std_men = [2, 3, 4, 1, 2]
std_women = [3, 5, 2, 3, 3]

groupedbar(["G1", "G2", "G3", "G4", "G5"], [men women], yerr = [std_men std_women])
```

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [October 24, 2017, 3:18pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/9 "2017-10-24T15:18:59Z")

</div>

There is a tiny example with grouping in `groupedbar` in the StatPlots README, right below the matrix example, showing how to use the `group` syntax, but admittedly it’s easy to overlook.

@kaslusimoes : it’s shameless self-advertising, but if you need to do some data preprocessing (for example compute `mean` and `sem` or `std`, or some other type of error across all observation or across some variable) before the plot, you could check out the `groupedbar` examples from [GroupedErrors](https://github.com/piever/GroupedErrors.jl)

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [November 18, 2019, 4:05pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/10 "2019-11-18T16:05:22Z")

</div>

In this last form, how can you specify the legend for different color bars ?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [November 18, 2019, 4:07pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/11 "2019-11-18T16:07:15Z")

</div>

Sorry, I could not find the doc but the solution is trivial : add `,label=["first","second"]`.

---

<div class="post-metadata">

**Author:** ![Parazetamol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/parazetamol/32/52125_2.png) [@Parazetamol](https://discourse.julialang.org/u/Parazetamol)\
**Post date:** [September 19, 2020, 8:10pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/12 "2020-09-19T20:10:27Z")

</div>

Hi there, I desperately try to label the individual bars, as in the original example 20, 25, … etc. I tried adding series\_annotations like this

```julia
using StatsPlots

men = [20, 35, 30, 35, 27]
women = [25, 32, 34, 20, 25]
std_men = [2, 3, 4, 1, 2]
std_women = [3, 5, 2, 3, 3]

groupedbar(
  ["G1", "G2", "G3", "G4", "G5"],
  [men women],
  yerr = [std_men std_women],
  series_annotations = [string.(men), string.(women)]
)

```

results in a plot with misaligned annotations:  
 ![Screenshot from 2020-09-19 22-17-31](https://global.discourse-cdn.com/julialang/original/3X/6/2/62eef7ed3542b7f7e9207fe1b06d607fd47df600.png)  
A second run results in a plot with the top cut off and an error message  
“GKS: Rectangle definition is invalid in routine SET\_VIEWPORT” .  
Please help!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 19, 2020, 8:46pm UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/13 "2020-09-19T20:46:31Z")

</div>

Welcome to Julia!

Please refrain from posting on an old thread for new help. Please open a new thread for this question.

Additionally, please read [PSA: Make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757). In particular, your code is not an MWE. We can’t help you without a reproducible example.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 20, 2020, 8:30am UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/14 "2020-09-20T08:30:20Z")

</div>

Note the comma in the `series_annotations` vector - this will create a column rather than a row vector, which is the wrong orientation for Plots.

```julia
julia> [1 2]
1×2 Array{Int64,2}:
 1 2

julia> [1, 2]
2-element Array{Int64,1}:
 1
 2

```

So try without the comma in your labels.

---

<div class="post-metadata">

**Author:** ![Parazetamol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/parazetamol/32/52125_2.png) [@Parazetamol](https://discourse.julialang.org/u/Parazetamol)\
**Post date:** [September 20, 2020, 9:46am UTC](https://discourse.julialang.org/t/plots-bars-side-by-side/6655/15 "2020-09-20T09:46:58Z")

</div>

Thanks for your answer. Sadly it doesn’t help.

> [men, women]

gives a

> 2-element Array{Array{Int64,1},1}

which I find appropriate for a groupedbar.

But let’s switch over to the new topic I created at [pdeffebach](https://discourse.julialang.org/u/pdeffebach)’s request:  
[https://discourse.julialang.org/t/how-to-annotate-a-groupedbar-plot/46938](https://discourse.julialang.org/t/how-to-annotate-a-groupedbar-plot/46938)
