# How to assign colors to bar plots in StatsPlots / Plots

**URL:** https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060
**Category:** General Usage
**Tags:** plotting
**Created:** [April 26, 2021, 8:04pm UTC](https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060 "2021-04-26T20:04:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 26, 2021, 8:04pm UTC](https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060/1 "2021-04-26T20:04:27Z")

</div>

Hi all,

How do I assign colors to individual bars with `StatsPlots`. The following used to work with `color`. However something changed with `Plots` and now it does not work.

```julia
using DataFrames, StatsPlots
df = DataFrame(a = ["a","b"], b = [1,2])
@df df bar(:a, :b, colorfill=[:red, :blue])

```

The following works with `color` and row vectors, but this is not compatible with `DataFrames`

```julia
using DataFrames, StatsPlots
df = DataFrame(a = ["a","b"], b = [1,2])
bar(["a" "b"], [1 2], color=[:red :blue])

```

Version:  
Plots v1.12.0  
StatsPlots v0.14.19

---

<div class="post-metadata">

### Author: ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)
#### Post date: [April 26, 2021, 8:09pm UTC](https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060/2 "2021-04-26T20:09:35Z")

</div>

Try

```julia
df = DataFrame(a = ["a","b"], b = [1,2], c = [:red, :blue])
@df df bar(:a, :b, color=:c)

```

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 26, 2021, 8:32pm UTC](https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060/3 "2021-04-26T20:32:43Z")

</div>

> [@mariok90](#):
>
> ```julia
> df = DataFrame(a = ["a","b"], b = [1,2], c = [:red, :blue])
> @df df bar(:a, :b, color=:c)
> 
> ```

Thank you for your response. Unfortunately, that does not fix the problem. I receive the following error message:

```julia
 Warning: Indices Base.OneTo(2) of attribute `markercolor` does not match data indices 1:11.
└ @ Plots ~/.julia/packages/Plots/kyYZF/src/utils.jl:102
┌ Info: Data contains NaNs or missing values, and indices of `markercolor` vector do not match data indices.
│ If you intend elements of `markercolor` to apply to individual NaN-separated segements in the data,
│ pass each segment in a separate vector instead, and use a row vector for `markercolor`. Legend entries 
│ may be suppressed by passing an empty label.
│ For example,
└ plot([1:2,1:3], [[4,5],[3,4,5]], label=["y" ""], markercolor=[1 2])

```

Replacing `color` with `fillcolor` does not help either.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 27, 2021, 9:12am UTC](https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060/4 "2021-04-27T09:12:39Z")

</div>

I opened an [issue](https://github.com/JuliaPlots/StatsPlots.jl/issues/442) since it appears to be a bug.

---

<div class="post-metadata">

### Author: ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)
#### Post date: [April 27, 2021, 9:17am UTC](https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060/5 "2021-04-27T09:17:11Z")

</div>

I got the same error once I updated Plots to version 1.12.0.

---

<div class="post-metadata">

### Author: ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)
#### Post date: [August 2, 2021, 9:55am UTC](https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060/6 "2021-08-02T09:55:43Z")

</div>

> [@Christopher\_Fisher](#):
>
> ```julia
> df = DataFrame(a = ["a","b"], b = [1,2])
> @df df bar(:a, :b, colorfill=[:red, :blue])
> 
> ```

The problem persists (also for Dictionaries). A **workaround** for DataFrames is:

```julia
@df df bar([[i] for i in :a], [[i] for i in :b], c=[:red :blue])

```

Notice the row vector for colors.  
A graphical problem is that gaps between bars are greater than usual bar plots.

For Dictionaries the workaround is similar:

```julia
data= Dict("Item 1" => 30,
           "Item 2" => 35,
           "Item 3" => 25)
bar([[i] for i in keys(data)], [[i] for i in values(data)], legend=false, c=[:red3 :salmon :skyblue])

```

but it used to be much simpler:

```julia
bar(data, legend=false, c=[:red3, :salmon, :skyblue])

```

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [August 2, 2021, 2:52pm UTC](https://discourse.julialang.org/t/how-to-assign-colors-to-bar-plots-in-statsplots-plots/60060/7 "2021-08-02T14:52:22Z")

</div>

Thank you for the solution.

I agree that it used to be much simpler. One of the problems is that it is now cumbersome for users to correctly assign the colors. I can imagine how this may lead to incorrect color assignment with datasets that have more factors and levels.
