# Single colorbar for heatmap grid layout

**URL:** <https://discourse.julialang.org/t/single-colorbar-for-heatmap-grid-layout/50609>\
**Category:** General Usage\
**Tags:** question, plots\
**Created:** [November 23, 2020, 4:03am UTC](https://discourse.julialang.org/t/single-colorbar-for-heatmap-grid-layout/50609 "2020-11-23T04:03:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [November 23, 2020, 4:03am UTC](https://discourse.julialang.org/t/single-colorbar-for-heatmap-grid-layout/50609/1 "2020-11-23T04:03:48Z")

</div>

Take for example:

```julia
using Plots

plot(
    heatmap(rand(10,10)),
    heatmap(rand(10,10)),
    heatmap(rand(10,10)),
    heatmap(rand(10,10));
    layout=4
)

```

![heatmaps](https://global.discourse-cdn.com/julialang/original/3X/2/c/2cf347246a67cd336e03ff39458e1f5dbe18a8c0.png)

How could it be possible to have a single colorbar for all heatmaps?

---

<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:** [November 23, 2020, 7:42am UTC](https://discourse.julialang.org/t/single-colorbar-for-heatmap-grid-layout/50609/2 "2020-11-23T07:42:51Z")

</div>

If you don’t mind trying out a different package, this is one of the problems why I wrote MakieLayout [http://makie.juliaplots.org/stable/makielayout/tutorial.html](http://makie.juliaplots.org/stable/makielayout/tutorial.html)

Other than that, I think people have come up with hacks to solve your problem in Plots, too, maybe you can find other threads in this discourse as well

---

<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:** [November 23, 2020, 7:51am UTC](https://discourse.julialang.org/t/single-colorbar-for-heatmap-grid-layout/50609/3 "2020-11-23T07:51:30Z")

</div>

Yes this isn’t easily possible in Plots.jl, as the plots are produced independently and there aren’t currently any figure level arguments (like figure title, legend etc.)

The usual hacks that I think Jules refers to are basically adding another subplot to your figure, which would just have the colorbar, and then do `legend = :none` on the heatmaps.

If you don’t mind one subplot being sized differently you could just do:

```julia
plot([[heatmap(rand(10, 10), legend = :none) for _ ∈ 1:3]; heatmap(rand(10, 10))]...)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/0/9/09eba9c97df1f30e45122bd38062b20f559df2ee.png)

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [November 23, 2020, 10:36am UTC](https://discourse.julialang.org/t/single-colorbar-for-heatmap-grid-layout/50609/4 "2020-11-23T10:36:23Z")

</div>

This does what you want but not directly.

```julia
julia> ps = [heatmap(rand(10,10), legend=:none) for i=1:2,j=1:2] # Make heatmaps without legends
2×2 Matrix{Plots.Plot{Plots.GRBackend}}:
 Plot{Plots.GRBackend() n=1} Plot{Plots.GRBackend() n=1}
 Plot{Plots.GRBackend() n=1} Plot{Plots.GRBackend() n=1}

julia> l = @layout[grid(2,2) a{0.05w}] # Stack a layout that rightmost one is for color bar
Plots.GridLayout(1, 2)

julia> p = plot(ps..., heatmap((0:0.01:1).*ones(101,1), legend=:none, xticks=:none, yticks=(1:10:101, string.(0:0.1:1))), layout=l) # Plot them set y values of color bar accordingly

julia> savefig(p, "plot.png")

```

![plot](https://global.discourse-cdn.com/julialang/original/3X/4/5/45bac3953a75903c2cb121e0f436b52e1b0855c2.png)

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [November 23, 2020, 10:54am UTC](https://discourse.julialang.org/t/single-colorbar-for-heatmap-grid-layout/50609/5 "2020-11-23T10:54:29Z")

</div>

> [@tomaklutfu](#):
>
> `heatmap((0:0.01:1).*ones(101,1), legend=:none, xticks=:none, yticks=(1:10:101, string.(0:0.1:1)))`

Yes!! I was trying to do the same `@layout` trick, but couldn’t figure out how to make the single large colorbar. A smooth heatmap is very clever!
