# How to link color axes of heatmaps?

**URL:** <https://discourse.julialang.org/t/how-to-link-color-axes-of-heatmaps/62511>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [June 7, 2021, 12:02pm UTC](https://discourse.julialang.org/t/how-to-link-color-axes-of-heatmaps/62511 "2021-06-07T12:02:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 7, 2021, 12:02pm UTC](https://discourse.julialang.org/t/how-to-link-color-axes-of-heatmaps/62511/1 "2021-06-07T12:02:08Z")

</div>

For example,

```julia
using CairoMakie
function testfig()
    fig = Figure()
    heatmaps = []
    for i = 1:4
        h = heatmap!(Axis(fig[i, 1]), i .+ rand(200, 200))
        push!(heatmaps, h)
    end
    Colorbar(fig[:, 2], heatmaps[1]; width=25)
    return fig
end

```

yields

 ![test](https://global.discourse-cdn.com/julialang/original/3X/c/8/c82815edcf5a800a9269ed68e4182a1b5363cacb.jpeg)

However, I’d like to link all the heatmaps to the colorscale, not just the first one. How can I do that?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 7, 2021, 12:06pm UTC](https://discourse.julialang.org/t/how-to-link-color-axes-of-heatmaps/62511/2 "2021-06-07T12:06:00Z")

</div>

I think a manual option is:

```julia
function testfig(; colorrange)
    fig = Figure()
    heatmaps = []
    for i = 1:4
        h = heatmap!(Axis(fig[i, 1]), i .+ rand(200, 200); colorrange)
        push!(heatmaps, h)
    end
    Colorbar(fig[:, 2]; limits=colorrange, width=25)
    return fig
end
save("test.png", testfig(; colorrange=(1, 5)))

```

which yields

 ![test](https://global.discourse-cdn.com/julialang/original/3X/6/0/607176542a7c035a0928f61e4e7786161a199e65.jpeg)

---

<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:** [June 7, 2021, 12:22pm UTC](https://discourse.julialang.org/t/how-to-link-color-axes-of-heatmaps/62511/3 "2021-06-07T12:22:54Z")

</div>

There’s currently no other option of linking colorranges than what you suggested yourself. This is because axes have more infrastructure to facilitate linking, while you could only couple the colorrange observables together, but that would be kind of hacky

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [June 7, 2021, 12:23pm UTC](https://discourse.julialang.org/t/how-to-link-color-axes-of-heatmaps/62511/4 "2021-06-07T12:23:11Z")

</div>

From [https://makie.juliaplots.org/stable/makielayout/tutorial.html#Adding-a-colorbar](https://makie.juliaplots.org/stable/makielayout/tutorial.html#Adding-a-colorbar) it seems that’s the right way to do it, except you might still want to do `Colorbar(fig[:, 2], heatmaps[1]; width=25)` to pick up the colormap from one of the heatmaps (giving the same `colorrange` to both heatmaps should be enough).

---

<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 7, 2021, 12:30pm UTC](https://discourse.julialang.org/t/how-to-link-color-axes-of-heatmaps/62511/5 "2021-06-07T12:30:01Z")

</div>

Slightly less manual would be to use `[1.05 -0.05; -0.05 1.05]*[minimum(minimum.(data)), maximum(maximum.(data))]`.

---

<div class="post-metadata">

**Author:** ![behinger](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@behinger](https://discourse.julialang.org/u/behinger)\
**Post date:** [March 25, 2025, 12:37pm UTC](https://discourse.julialang.org/t/how-to-link-color-axes-of-heatmaps/62511/6 "2025-03-25T12:37:46Z")

</div>

Is there a 2025 solution to this problem?
