# One colorbar for all subplots using cartopy mapping via PyCall

**URL:** <https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134>\
**Category:** Visualization\
**Tags:** plotting, pyplot, geo\
**Created:** [January 28, 2021, 4:58pm UTC](https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134 "2021-01-28T16:58:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![EuRoXy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euroxy/32/13970_2.png) [@EuRoXy](https://discourse.julialang.org/u/EuRoXy)\
**Post date:** [January 28, 2021, 4:58pm UTC](https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134/1 "2021-01-28T16:58:11Z")

</div>

I try to generate a shared colorbar for several subplots using cartopy via PyCall and PyPlot but haven’t succeeded. Here’s a minimal code example with separate colorbars:

```julia
using PyCall, PyPlot

plt = pyimport("matplotlib.pyplot")
ccrs = pyimport("cartopy.crs")
cfeature = pyimport("cartopy.feature")
cput = pyimport("cartopy.util")

proj1 = ccrs.PlateCarree();
proj3 = ccrs.EuroPP();

lons = [lon for lon in -10.:2.5:27.5]
arr = rand(16,16,2)

fig = plt.figure()
for i in 1:2
    ax = plt.subplot(1,2,i, projection=proj3)
    wrap_data, wrap_lon = cput.add_cyclic_point(arr[:,:,i], coord=lons, axis=1)
    ax.add_feature(cfeature.OCEAN, zorder=100, edgecolor="k", lw=.5, facecolor=cfeature.COLORS["land"]) 
    p = ax.pcolormesh(wrap_lon, lat, wrap_data, transform=proj1, cmap="RdBu_r")
    cbar = plt.colorbar(p, shrink=0.9, orientation="horizontal") 
end
plt.tight_layout()

```

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

My main problem is that `p` is only valid inside the loop, once I move `cbar` out it gives error, and I haven’t had an idea how to deal with it … Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [January 28, 2021, 5:40pm UTC](https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134/2 "2021-01-28T17:40:19Z")

</div>

> [@EuRoXy](#):
>
> My main problem is that `p` is only valid inside the loop,

You are using Julia V1.4 or earlier, right?  
With Julia 1.5 it should work out-of-the-box because the interactive scoping rules have been changed.  
In earlier Julia versions, you need to add

```julia
global p

```

before the for loop, or wrap the whole code into a function.

---

<div class="post-metadata">

**Author:** ![EuRoXy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euroxy/32/13970_2.png) [@EuRoXy](https://discourse.julialang.org/u/EuRoXy)\
**Post date:** [January 28, 2021, 5:51pm UTC](https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134/3 "2021-01-28T17:51:57Z")

</div>

Thank you, I am using V1.5.3 actually, but `global p` indeed works. Though I still need to figure out how to scale the colorbar. Do you have good suggestions?

```julia
arr = rand(16,16,4)

fig = plt.figure()
for i in 1:4
    ax = plt.subplot(2,2,i, projection=proj3)
    wrap_data, wrap_lon = cput.add_cyclic_point(arr[:,:,i], coord=lons, axis=1)
    ax.add_feature(cfeature.OCEAN, zorder=100, edgecolor="k", lw=.5, facecolor=cfeature.COLORS["land"]) 
    global p = ax.pcolormesh(wrap_lon, lat, wrap_data, transform=proj1, cmap="RdBu_r")
end
plt.tight_layout()
cbar = plt.colorbar(p, orientation="horizontal") 

```

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

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [January 28, 2021, 6:33pm UTC](https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134/4 "2021-01-28T18:33:05Z")

</div>

The Julia 1.5 scoping has only been changed for interactive usage in the REPL, not when your code is in a file. But you should have received a warning message about the ambiguity of the variable `p`.

Regarding the color bar scaling I unfortunately cannot help you.

---

<div class="post-metadata">

**Author:** ![EuRoXy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euroxy/32/13970_2.png) [@EuRoXy](https://discourse.julialang.org/u/EuRoXy)\
**Post date:** [January 29, 2021, 9:35am UTC](https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134/5 "2021-01-29T09:35:11Z")

</div>

I searched for some py examples and found this would work:

```julia
fig.subplots_adjust(bottom=0.25)
cbar_ax = fig.add_axes([0.17, 0.17, 0.65, 0.04])
fig.colorbar(p, cax=cbar_ax, orientation="horizontal")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/0/2/023208a07bda6e04dd402e1ff613f549da38aecb.png)

Hopefully the interactive scoping would soon be available not only in the REPL.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [January 29, 2021, 12:50pm UTC](https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134/6 "2021-01-29T12:50:14Z")

</div>

> [@EuRoXy](#):
>
> Hopefully the interactive scoping would soon be available not only in the REPL.

No, they are good reasons for the current behavior, see here: [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#On-Soft-Scope)

---

<div class="post-metadata">

**Author:** ![EuRoXy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euroxy/32/13970_2.png) [@EuRoXy](https://discourse.julialang.org/u/EuRoXy)\
**Post date:** [January 29, 2021, 4:16pm UTC](https://discourse.julialang.org/t/one-colorbar-for-all-subplots-using-cartopy-mapping-via-pycall/54134/7 "2021-01-29T16:16:04Z")

</div>

OIC, thanks for the helpful info.
