# How to set labels for eached coloured bar in Plots.jl's bar()?

**URL:** https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312
**Category:** Visualization
**Created:** [February 25, 2018, 10:29am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312 "2018-02-25T10:29:52Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [February 25, 2018, 10:29am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/1 "2018-02-25T10:29:52Z")

</div>

```julia
bar(
    ["a","b","c"],
    [1,2,3],
    fillcolor = categorical(["a","b","c"]).refs,
    labels = ["a","b","c"]
)

```

I can make the plot with the above code, but I can’t seem to work out how to make the labels follow the same colours and text. I tried doing a `group=` but the `bar_position = :dodge` isn’t implemented.

 ![test](https://global.discourse-cdn.com/julialang/original/3X/e/8/e86a1909c3ccd688d0395a71345ff3b4f557587f.png)

---

<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: [February 25, 2018, 10:47am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/2 "2018-02-25T10:47:31Z")

</div>

Have you tried [groupedbar](https://github.com/JuliaPlots/StatPlots.jl/blob/master/README.md#grouped-bar-plots) instead?

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [February 25, 2018, 10:48am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/3 "2018-02-25T10:48:22Z")

</div>

Yes. It’s causing an error see [https://github.com/JuliaPlots/StatPlots.jl/issues/129](https://github.com/JuliaPlots/StatPlots.jl/issues/129)

---

<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: [February 25, 2018, 1:53pm UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/4 "2018-02-25T13:53:09Z")

</div>

In principle it should be possible to just pass each bar as a separate series:

```julia
bar(
    ["a" "b" "c"],
    [1 2 3],
    labels = ["a" "b" "c"]
)

```

I say “in principle” because there appears to be a problem with passing just one series at a time.  
So the way you’re doing it should be the best workaround. The only thing that doesn’t seem to work is the legend, which you want to say the exact same thing as the x labels. Could I ask you why? When would you do that?

---

<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: [February 25, 2018, 3:06pm UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/5 "2018-02-25T15:06:58Z")

</div>

I filed the issue: the problem is that the series with only one bar (that one would get by grouping) doesn’t work.

I find the legend generally not very useful when working with bar plots (the information is generally on the axis already) but it helped us uncover a few things to fix in Plots. I’ve opened an [umbrella issue](https://github.com/JuliaPlots/Plots.jl) at Plots about horizontal layout.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [February 25, 2018, 9:07pm UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/6 "2018-02-25T21:07:43Z")

</div>

Actually in my benchmarks I want those from Julia to be of a different colour to those from R

---

<div class="post-metadata">

### Author: ![daschw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daschw/32/2926_2.png) [@daschw](https://discourse.julialang.org/u/daschw)
#### Post date: [February 26, 2018, 9:18am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/7 "2018-02-26T09:18:50Z")

</div>

```julia
bar(
    ["a" "b" "c"],
    [1 2 3],
    labels = ["a" "b" "c"],
    bar_width = 1
)

```

is already possible. [https://github.com/JuliaPlots/Plots.jl/pull/1414](https://github.com/JuliaPlots/Plots.jl/pull/1414) allows

```julia
bar(
    ["a" "b" "c"],
    [1 2 3],
    labels = ["a" "b" "c"],
)

```

as well.

---

<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: [February 26, 2018, 9:19am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/8 "2018-02-26T09:19:34Z")

</div>

Great @piever!  
@xiaodai for sure, I was just wondering why you’d want to duplicate that information in the legend.

---

<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: [February 26, 2018, 9:20am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/9 "2018-02-26T09:20:17Z")

</div>

Awesome @daschw 🙂 Nice timing on the comment…

---

<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: [February 26, 2018, 10:16am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/10 "2018-02-26T10:16:43Z")

</div>

Good point, hadn’t thought about helping Plots find the default `bar_width`. Nice job with the fix!

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 2, 2020, 9:16am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/11 "2020-03-02T09:16:17Z")

</div>

A bit overkilled for this, but (now?) in StatsPlots you can use `group` to group the serie over any given array:

For bars:

```julia
vals = [1,2,3]
cats = ["a","b","c"]
colorMap = Dict("a"=>"red","b"=>"blue","c"=>"green")
labelMap = Dict("a"=>"cat a","b"=>"cat b","c"=>"cat c")
colours = [colorMap[i] for i in cats]
labels = [labelMap[i] for i in cats]
bar(cats,vals,colour=colours,group=labels, legend=:topleft)

```

![barexample](https://global.discourse-cdn.com/julialang/original/3X/9/6/964699eae22d2bf12e9be95b7f7e6d2fc307b734.png)

A case where this approach could be more useful:

```julia
colors = [y == 1 ? "blue" : "red" for y in y]
labels = [y == 1 ? "benign" : "malign" for y in y]
scatter(X[:,1],X[:,2], colour=colors, title="Classified tumors",xlabel="Tumor Radius", ylabel="Tumor Texture", group=labels)

```

![cancerplot](https://global.discourse-cdn.com/julialang/original/3X/0/1/01f539cc11171f37bb4531bb87fa35280e1b348e.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: [March 2, 2020, 11:00am UTC](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/12 "2020-03-02T11:00:28Z")

</div>

I’m not sure what the idea is here?
