# Independent custom colorbar in Plots?

**URL:** https://discourse.julialang.org/t/independent-custom-colorbar-in-plots/91479
**Category:** General Usage
**Tags:** plotting, plots
**Created:** [December 9, 2022, 2:28pm UTC](https://discourse.julialang.org/t/independent-custom-colorbar-in-plots/91479 "2022-12-09T14:28:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sim/32/22207_2.png) [@sim](https://discourse.julialang.org/u/sim)
#### Post date: [December 9, 2022, 2:28pm UTC](https://discourse.julialang.org/t/independent-custom-colorbar-in-plots/91479/1 "2022-12-09T14:28:57Z")

</div>

Hi, let’s suppose I have the following (essentially empty) plot:

```julia
plot(xlabel="x axis", ylabel="y axis")
plot!(rand(10))

```

![dummy](https://global.discourse-cdn.com/julialang/original/3X/d/3/d39f4e777a0232354f3265b4200f5e143ad43e01.png)

Now, for some reason, I want to add a colorbar to this figure (yes, the colorbar has nothing to do with the plot… _yet_).  
I just want an independant colorbar, say on the right of the figure, showing some colormap, for example `cmap=cgrad(:thermal)`, with values ranging between 0 and 1.

I found many issues related to the colorbar itself, but I could not understand how to create a custom colormap in Plots.jl. Any help appreciated !

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 9, 2022, 3:22pm UTC](https://discourse.julialang.org/t/independent-custom-colorbar-in-plots/91479/2 "2022-12-09T15:22:42Z")

</div>

A workaround using layouts:

```julia
using Plots; gr()
l = @layout [a{0.95w} b]
cmap = cgrad(:thermal)
p1 = plot(rand(10), xlabel="x axis", ylabel="y axis")
p2 = heatmap(rand(2,2), clims=(0,1), framestyle=:none, c=cmap, cbar=true, lims=(-1,0))
plot(p1, p2, layout=l)

```

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [December 9, 2022, 3:35pm UTC](https://discourse.julialang.org/t/independent-custom-colorbar-in-plots/91479/3 "2022-12-09T15:35:50Z")

</div>

And with GMT

```julia
using GMT

C = makecpt(range=(0,1), cmap=:turbo);
plot(rand(10), lc=:blue, xlabel="x axis", ylabel="y axis", colorbar=true, show=true)

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/e/6/e68b2d2d35daeea0b47f7fbb9690a4933a235cac.png)

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [December 9, 2022, 6:39pm UTC](https://discourse.julialang.org/t/independent-custom-colorbar-in-plots/91479/4 "2022-12-09T18:39:23Z")

</div>

If we’re adding how other plotting packages handle this, this is a good example how Makie does these things a bit differently and you add a colorbar explicitly instead of using a plotting command as a workaround:

```julia
using CairoMakie

f, ax, sc = lines(rand(10))
cb = Colorbar(f[1, 2], colormap = :turbo, colorrange = (0, 1))
f

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/9/0/906b77c7f0908a4f5888c281295fdf9a03640133.png)
