# Plots.jl: shared colorbar with subplots?

**URL:** <https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [September 25, 2020, 3:05pm UTC](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269 "2020-09-25T15:05:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielIngraham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielingraham/32/13055_2.png) [@DanielIngraham](https://discourse.julialang.org/u/DanielIngraham)\
**Post date:** [September 25, 2020, 3:05pm UTC](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269/1 "2020-09-25T15:05:19Z")

</div>

I’d like to be able to create an array of subplots with a shared colorbar, i.e., just one colorbar for all subplots. Is that possible?

```julia
using Plots

function doit()
    x1 = range(0.0, 1.0, length=51)
    y1 = range(-1.0, 0.0, length=51)
    z1 = sin.(4*pi.*x1)*cos.(6*pi.*reshape(y1, 1, :))

    x2 = range(0.25, 1.25, length=51)
    y2 = range(-1.0, 0.0, length=51)
    z2 = 2.0.*sin.(4*pi.*x2)*cos.(6*pi.*reshape(y2, 1, :))

    x3 = range(0.0, 1.0, length=51)
    y3 = range(-1.25, -0.25, length=51)
    z3 = 3.0*sin.(4*pi.*x3)*cos.(6*pi.*reshape(y3, 1, :))

    x4 = range(0.25, 1.25, length=51)
    y4 = range(-1.25, -0.25, length=51)
    z4 = 4.0.*sin.(4*pi.*x4)*cos.(6*pi.*reshape(y4, 1, :))

    clims = extrema([z1; z2; z3; z4])
    p1 = contourf(x1, y1, z1, clims=clims, c=:viridis, colorbar=false)
    p2 = contourf(x2, y2, z2, clims=clims, c=:viridis, colorbar=false)
    p3 = contourf(x3, y3, z3, clims=clims, c=:viridis, colorbar=false)
    p4 = contourf(x4, y4, z4, clims=clims, c=:viridis, colorbar=true)

    p_all = plot(p1, p2, p3, p4, layout=(2, 2), link=:all)
    savefig(p_all, "shared_colorbar_julia.png")
end

```

The resulting figure is this:  
 ![shared_colorbar_julia](https://global.discourse-cdn.com/julialang/original/3X/f/f/fff2f5ab74fd37ed75165d28eeb0a8c08579d3d3.png)  
What I’d like is one long colorbar to the right of the subplots, that extends from the top of the upper row to the bottom of the lower row.

Thanks!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 25, 2020, 3:13pm UTC](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269/2 "2020-09-25T15:13:20Z")

</div>

Something like this might work: [https://github.com/JuliaPlots/Plots.jl/issues/412#issuecomment-249813635](https://github.com/JuliaPlots/Plots.jl/issues/412#issuecomment-249813635)

---

<div class="post-metadata">

**Author:** ![DanielIngraham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielingraham/32/13055_2.png) [@DanielIngraham](https://discourse.julialang.org/u/DanielIngraham)\
**Post date:** [September 25, 2020, 6:51pm UTC](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269/3 "2020-09-25T18:51:13Z")

</div>

Excellent, thanks. This is what I have so far:

```julia
function doit2()
    x1 = range(0.0, 1.0, length=51)
    y1 = range(-1.0, 0.0, length=51)
    z1 = sin.(4*pi.*x1)*cos.(6*pi.*reshape(y1, 1, :))

    x2 = range(0.25, 1.25, length=51)
    y2 = range(-1.0, 0.0, length=51)
    z2 = 2.0.*sin.(4*pi.*x2)*cos.(6*pi.*reshape(y2, 1, :))

    x3 = range(0.0, 1.0, length=51)
    y3 = range(-1.25, -0.25, length=51)
    z3 = 3.0*sin.(4*pi.*x3)*cos.(6*pi.*reshape(y3, 1, :))

    x4 = range(0.25, 1.25, length=51)
    y4 = range(-1.25, -0.25, length=51)
    z4 = 4.0.*sin.(4*pi.*x4)*cos.(6*pi.*reshape(y4, 1, :))

    clims = extrema([z1; z2; z3; z4])
    p1 = contourf(x1, y1, z1, clims=clims, c=:viridis, colorbar=false)
    p2 = contourf(x2, y2, z2, clims=clims, c=:viridis, colorbar=false)
    p3 = contourf(x3, y3, z3, clims=clims, c=:viridis, colorbar=false)
    p4 = contourf(x4, y4, z4, clims=clims, c=:viridis, colorbar=false)

    h2 = scatter([0,0], [0,1], zcolor=[0,3], clims=clims,
                 xlims=(1,1.1), xshowaxis=false, yshowaxis=false, label="", c=:viridis, colorbar_title="cbar", grid=false)

    @show h2[1][:xaxis][:showaxis]
    @show h2[1][:yaxis][:showaxis]

    l = @layout [grid(2, 2) a{0.01w}]
    p_all = plot(p1, p2, p3, p4, h2, layout=l)
    savefig(p_all, "shared_colorbar_julia2.png")
end

```

This gives me this plot:  
 ![shared_colorbar_julia2](https://global.discourse-cdn.com/julialang/original/3X/0/c/0cdf8462844e0f651dde1263f90ae49358550353.png)

Is there a way to get rid of the x and y axis labels? I have `xshowaxis=false` and `yshowaxis=false`, but that doesn’t do the trick.

Thanks again!

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 25, 2020, 7:10pm UTC](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269/4 "2020-09-25T19:10:57Z")

</div>

`framestye = :none` should work

---

<div class="post-metadata">

**Author:** ![DanielIngraham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielingraham/32/13055_2.png) [@DanielIngraham](https://discourse.julialang.org/u/DanielIngraham)\
**Post date:** [September 25, 2020, 7:20pm UTC](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269/5 "2020-09-25T19:20:11Z")

</div>

Great, that seemed to do it. Here’s what I ended up with:

```julia
using Plots
function doit2()
    x1 = range(0.0, 1.0, length=51)
    y1 = range(-1.0, 0.0, length=51)
    z1 = sin.(4*pi.*x1)*cos.(6*pi.*reshape(y1, 1, :))

    x2 = range(0.25, 1.25, length=51)
    y2 = range(-1.0, 0.0, length=51)
    z2 = 2.0.*sin.(4*pi.*x2)*cos.(6*pi.*reshape(y2, 1, :))

    x3 = range(0.0, 1.0, length=51)
    y3 = range(-1.25, -0.25, length=51)
    z3 = 3.0*sin.(4*pi.*x3)*cos.(6*pi.*reshape(y3, 1, :))

    x4 = range(0.25, 1.25, length=51)
    y4 = range(-1.25, -0.25, length=51)
    z4 = 4.0.*sin.(4*pi.*x4)*cos.(6*pi.*reshape(y4, 1, :))

    clims = extrema([z1; z2; z3; z4])
    p1 = contourf(x1, y1, z1, clims=clims, c=:viridis, colorbar=false)
    p2 = contourf(x2, y2, z2, clims=clims, c=:viridis, colorbar=false)
    p3 = contourf(x3, y3, z3, clims=clims, c=:viridis, colorbar=false)
    p4 = contourf(x4, y4, z4, clims=clims, c=:viridis, colorbar=false)

    h2 = scatter([0,0], [0,1], zcolor=[0,3], clims=clims,
                 xlims=(1,1.1), label="", c=:viridis, colorbar_title="cbar", framestyle=:none)

    l = @layout [grid(2, 2) a{0.035w}]
    p_all = plot(p1, p2, p3, p4, h2, layout=l, link=:all)
    savefig(p_all, "shared_colorbar_julia2.png")
end

```

The plot:  
 ![shared_colorbar_julia2](https://global.discourse-cdn.com/julialang/original/3X/5/4/54cc38596d5eb785917988c246c9bf9ad36c81a0.png)

Thanks everyone!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 25, 2020, 9:15pm UTC](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269/6 "2020-09-25T21:15:07Z")

</div>

Cool that it worked! You should mark it as solved.

---

<div class="post-metadata">

**Author:** ![Gyoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyoshi/32/7119_2.png) [@Gyoshi](https://discourse.julialang.org/u/Gyoshi)\
**Post date:** [December 29, 2020, 5:19pm UTC](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269/7 "2020-12-29T17:19:23Z")

</div>

Here is a hack that lets you change the position and size of the colorbar via an invisible inset subplot, for lost souls like me trying to make this work with the pyplot backend:

```julia
x1 = range(0.0, 1.0, length=51)
y1 = range(-1.0, 0.0, length=51)
z1 = sin.(4*pi.*x1)*cos.(6*pi.*reshape(y1, 1, :))

x2 = range(0.25, 1.25, length=51)
y2 = range(-1.0, 0.0, length=51)
z2 = 2.0.*sin.(4*pi.*x2)*cos.(6*pi.*reshape(y2, 1, :))

x3 = range(0.0, 1.0, length=51)
y3 = range(-1.25, -0.25, length=51)
z3 = 3.0*sin.(4*pi.*x3)*cos.(6*pi.*reshape(y3, 1, :))

x4 = range(0.25, 1.25, length=51)
y4 = range(-1.25, -0.25, length=51)
z4 = 4.0.*sin.(4*pi.*x4)*cos.(6*pi.*reshape(y4, 1, :))

clims = extrema([z1; z2; z3; z4])
p1 = contourf(x1, y1, z1, clims=clims, c=:viridis, colorbar=false)
p2 = contourf(x2, y2, z2, clims=clims, c=:viridis, colorbar=false)
p3 = contourf(x3, y3, z3, clims=clims, c=:viridis, colorbar=false)
p4 = contourf(x4, y4, z4, clims=clims, c=:viridis, colorbar=false)

blank = plot(foreground_color_subplot=:white)
l = @layout [grid(2, 2) a{0.075w}]
plot(p1, p2, p3, p4, blank, layout=l, link=:all)
p_all = scatter!([0], [0], zcolor=[NaN], clims=clims, label="", c=:viridis, colorbar_title="cbar", background_color_subplot=:transparent, markerstrokecolor=:transparent, framestyle=:none, inset=bbox(0.1, 0, 0.6, 0.9, :center, :right), subplot=6)

```

![cbar_hack](https://global.discourse-cdn.com/julialang/original/3X/2/6/26de7dac163a361aadf7524a732e431c5885b45d.png)
