# Changing transparency of individual bars in a barplot using CairoMakie

**URL:** <https://discourse.julialang.org/t/changing-transparency-of-individual-bars-in-a-barplot-using-cairomakie/98250>\
**Category:** Visualization\
**Tags:** plotting, makie, cairomakie\
**Created:** [May 3, 2023, 1:07pm UTC](https://discourse.julialang.org/t/changing-transparency-of-individual-bars-in-a-barplot-using-cairomakie/98250 "2023-05-03T13:07:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhache](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bhache/32/15670_2.png) [@bhache](https://discourse.julialang.org/u/bhache)\
**Post date:** [May 3, 2023, 1:07pm UTC](https://discourse.julialang.org/t/changing-transparency-of-individual-bars-in-a-barplot-using-cairomakie/98250/1 "2023-05-03T13:07:55Z")

</div>

Hi, I am trying to generate a barplot using CairoMakie which would consist two sets of bars each with a different transparency. So far I am not sure how to achieve that.

MWE below depicts the barplot I am generating so far

```julia
import CairoMakie, Random;

n_A, n_B, n_C = 6, 2, 3;

fig = CairoMakie.Figure();
ax = CairoMakie.Axis(fig[1, 1],
        xlabel="xlabel", ylabel = "ylabel",
    );

barplot = let
    Random.seed!(0)
    data = rand(n_A, n_B * n_C)
    xs = repeat(1:n_A, n_B * n_C)
    dgd = repeat(repeat(1:n_B, inner=n_A), n_C)
    stk = clr = repeat(1:n_C, inner=n_A * n_B)
    CairoMakie.barplot!(
        ax,
        xs, data[:],
        dodge=dgd, stack=stk, color=clr,
        direction=:x,
    ) 
end;
display(fig)
CairoMakie.save("test.png", fig);

```

 ![test](https://global.discourse-cdn.com/julialang/original/3X/9/9/99c1b434661c555274435d23ba7f3fee676084d4.png)

In this barplot there are six pairs of stacked bars and I would like to have one transparency for the first stacked bar of the pairs and another for the second. I would really appreciate if one could point me in the right direction.

---

<div class="post-metadata">

**Author:** ![bhache](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bhache/32/15670_2.png) [@bhache](https://discourse.julialang.org/u/bhache)\
**Post date:** [May 4, 2023, 9:39am UTC](https://discourse.julialang.org/t/changing-transparency-of-individual-bars-in-a-barplot-using-cairomakie/98250/2 "2023-05-04T09:39:20Z")

</div>

I was able to achieve what I wanted using packages `Colors` and `ColorSchemes`. I extracted the colours using `get()` function to define a custom colormap which contains colours of two transparencies shown as below

```julia
import CairoMakie, Colors, ColorSchemes, Random;

n_A, n_B, n_C = 6, 2, 3;

fig = CairoMakie.Figure();
ax = CairoMakie.Axis(fig[1, 1],
        xlabel="xlabel", ylabel = "ylabel",
    );

barplot = let
    Random.seed!(0)
    data = rand(n_A, n_B * n_C)
    xs = repeat(1:n_A, n_B * n_C)
    dgd = repeat(repeat(1:n_B, inner=n_A), n_C)
    stk = repeat(1:n_C, inner=n_A * n_B)
    viridis = get(ColorSchemes.viridis, 1:n_buffers, (1, n_buffers))
    viridis_half = Colors.RGBA.(viridis, 0.5)
    custom_colormap = [viridis_half viridis]'[:]
    clr = repeat(custom_colormap, inner=6)
    CairoMakie.barplot!(
        ax,
        xs, data[:],
        dodge=dgd, stack=stk, color=clr,
        direction=:x,
    )
end;
display(fig)
CairoMakie.save("test.png", fig);

```

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

Though this solves the current issue, I was wondering if there is a cleaner and simpler way of achieving this.
