# Plotting simple bar charts with Plots.jl with colors conditional on sign of values

**URL:** <https://discourse.julialang.org/t/plotting-simple-bar-charts-with-plots-jl-with-colors-conditional-on-sign-of-values/63379>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [June 22, 2021, 3:04pm UTC](https://discourse.julialang.org/t/plotting-simple-bar-charts-with-plots-jl-with-colors-conditional-on-sign-of-values/63379 "2021-06-22T15:04:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfa\_julia\_quant](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@dfa\_julia\_quant](https://discourse.julialang.org/u/dfa_julia_quant)\
**Post date:** [June 22, 2021, 3:04pm UTC](https://discourse.julialang.org/t/plotting-simple-bar-charts-with-plots-jl-with-colors-conditional-on-sign-of-values/63379/1 "2021-06-22T15:04:23Z")

</div>

I would like to create a bar chart that shows negative values of different color from positive values. I created a very simple example using x = 6 discrete time periods and y = 6 financial payoffs (2 of which are negative).

I attempted to specify a color for each financial payoff; this failed. What happened instead is that it took the first color and applied it to all 6 payoffs as opposed to applying each color to each payoff in sequence.

The simple plot code I am using is as follows (done within a Pluto notebook):

using Plots  
x = 1:6 # This is a simple index across time periods 1 through 6.  
y = [-50, -25, 0, 25, 50, 100] # this shows financial payoffs for time periods 1 through 6.  
colors = [“#db5a44”, “#db5a44”, “#447adb”, “#447adb”, “#447adb”, “#447adb”]  
plot(x, y, seriestype = :bar, legend=false, size=(800,500), palette = colors)

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

What is the suggested way to introduce conditional coloring to the bar chart data, keeping it as simple as possible using only the Plots.jl package?

---

<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:** [June 22, 2021, 3:21pm UTC](https://discourse.julialang.org/t/plotting-simple-bar-charts-with-plots-jl-with-colors-conditional-on-sign-of-values/63379/2 "2021-06-22T15:21:38Z")

</div>

A color is set per series, as discussed here: [How to set labels for eached coloured bar in Plots.jl's bar()? - #6 by xiaodai](https://discourse.julialang.org/t/how-to-set-labels-for-eached-coloured-bar-in-plots-jls-bar/9312/6)

You could do:

```julia
julia> bar([[i] for i ∈ x], [[i] for i ∈ y], 
            color = permutedims(ifelse.(y .< 0, "#db5a44", "#447adb")), 
            label = "")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/3/83c0b54e525956ed0168dc627b80210722c0d26a.png)

---

<div class="post-metadata">

**Author:** ![dfa\_julia\_quant](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@dfa\_julia\_quant](https://discourse.julialang.org/u/dfa_julia_quant)\
**Post date:** [June 22, 2021, 5:55pm UTC](https://discourse.julialang.org/t/plotting-simple-bar-charts-with-plots-jl-with-colors-conditional-on-sign-of-values/63379/3 "2021-06-22T17:55:35Z")

</div>

> [@nilshg](#):
>
> ```julia
> bar([[i] for i ∈ x], [[i] for i ∈ y], 
> color = permutedims(ifelse.(y .< 0, "#db5a44", "#447adb")), 
> label = "")
> 
> ```

Excellent! This is exactly what I needed. Thank you nilshg.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 22, 2021, 7:18pm UTC](https://discourse.julialang.org/t/plotting-simple-bar-charts-with-plots-jl-with-colors-conditional-on-sign-of-values/63379/4 "2021-06-22T19:18:54Z")

</div>

```julia
plot(x[y .>= 0], y[y .>= 0]; seriestype=:bar, label="Positive")
plot!(x[y .< 0], y[y .< 0]; seriestype=:bar, label="Negative")

```

if you want them to be separate series with labels.
