# Heatmap with irregularly spaced grids

**URL:** <https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [March 1, 2022, 9:17pm UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259 "2022-03-01T21:17:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredrikpaues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikpaues/32/34080_2.png) [@fredrikpaues](https://discourse.julialang.org/u/fredrikpaues)\
**Post date:** [March 1, 2022, 9:17pm UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/1 "2022-03-01T21:17:50Z")

</div>

I’m trying to create a figure with two heatmaps where

1. the x and y variables are irregularly spaced,
2. the scale of the colors are the same in both heatmaps, and
3. there is only one colorbar.

In Python, I would do

```python
import numpy as np
import matplotlib.pyplot as plt

# Generate non-linear grid
n = 20
x = np.zeros(n)

x[0] = 0

for i in range(1, n):
    x[i] = x[i - 1] + (1 - x[i - 1]) / (n - i) ** 1.25

# Generate z variable
z = np.zeros((2, n, n))
for i in range(n):
    for j in range(n):
        r = x[i]**2 + x[j]**2
        z[0, i, j] = np.sin(10 * r) / (1 + r)

z[1] = z[0]

# Plot z variables against non-linear grids
x1, x2 = np.meshgrid(x, x, indexing='ij')
plt.subplot(211)
plt.pcolormesh(x1, x2, z[0], shading='auto')
plt.subplot(212)
plt.pcolormesh(x1, x2, z[1], shading='auto')

# Insert one colormap
plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
cax = plt.axes([0.85, 0.1, 0.075, 0.8])
plt.colorbar(cax=cax)

# Synchronize scale of colormap
zmin = np.minimum(z.min(), z2.min())
zmax = np.maximum(z.max(), z2.max())
for im in plt.gca().get_images():
    im.set_clim(vmin=zmin, vmax=zmax)

plt.show()

```

with result  
 ![Figure_1](https://global.discourse-cdn.com/julialang/original/3X/1/8/18835ae8de8d99ec18eec193e3c9c3abe80c9af1.png)

But in Julia I can’t figure out what to do. What I have thus far is

```julia
using Plots

# Generate non-linear grid
n = 20
x = zeros(n)
x[1] = 0
for i in 2:n
    x[i] = x[i - 1] + (1 - x[i - 1]) / (n - i + 1)^1.25
end

# Generate z variables
z = zeros(n, n)
for i in 1:n
    for j in 1:n
        r = x[i]^2 + x[j]^2
        z[i, j] = sin(10*r) / (1 + r)
    end
end
z2 = 0.5 .* z

# Plot z variables against the INDICES of the non-linear grids
plot(heatmap(z), heatmap(z2), layout=(2, 1))

```

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [March 1, 2022, 9:19pm UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/2 "2022-03-01T21:19:55Z")

</div>

Hi there! Check [this out 🙂](https://lazarusa.github.io/BeautifulMakie/heatmaps/heatmapIrregular/)

---

<div class="post-metadata">

**Author:** ![fredrikpaues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikpaues/32/34080_2.png) [@fredrikpaues](https://discourse.julialang.org/u/fredrikpaues)\
**Post date:** [March 1, 2022, 9:24pm UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/3 "2022-03-01T21:24:14Z")

</div>

Great! That takes care of item 1. As for item 3, [this](https://discourse.julialang.org/t/single-colorbar-for-heatmap-grid-layout/50609/4) probably fixes it. That leaves item 2 🤓

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [March 1, 2022, 9:31pm UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/4 "2022-03-01T21:31:51Z")

</div>

I would do it all in [Makie](https://makie.juliaplots.org/stable/). Note that the discourse thread you linked to uses Plots, which is a different plotting package. If you choose to use Makie, look at the color bar docs, you have a lot of control [over placement, scale etc.](https://makie.juliaplots.org/stable/examples/layoutables/colorbar/index.html)

Makie has a slight learning curve, but it is well worth it 🙂

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [March 1, 2022, 9:44pm UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/5 "2022-03-01T21:44:54Z")

</div>

lmk if you want me to implement it in Makie!

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [March 1, 2022, 9:51pm UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/6 "2022-03-01T21:51:33Z")

</div>

+1 for Makie. There was a recent post here regarding your second point, including some discussion about making the API for it even simpler: [One colorbar for multiple axes - #6 by ederag](https://discourse.julialang.org/t/one-colorbar-for-multiple-axes/77021/6)

---

<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:** [March 1, 2022, 10:55pm UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/7 "2022-03-01T22:55:31Z")

</div>

> [@fredrikpaues](#):
>
> ```julia
> But in Julia I can't figure out what to do...
> using Plots
> 
> ```

One way of achieving this using Plots.jl is [shown here](https://discourse.julialang.org/t/plots-jl-shared-colorbar-with-subplots/47269/5) and adapted below.

```julia
# 1 - INPUT DATA: generate non-linear grid and z variables
n = 20
x = zeros(n)
for i in 2:n
    x[i] = x[i-1] + (1 - x[i-1]) / (n-i+1)^1.25
end
r = @. x^2 + x'^2
z = @. sin(10*r) / (1 + r)
z2 = 0.5z

# 2 - PLOT DATA: multiple heatmaps with single colorbar
using Plots; gr()
clims = extrema([z; z2])
p1 = heatmap(x, x, z, ratio=1, clims=clims, cb=false, lims=extrema(x), tick_dir=:out)
p2 = heatmap(x, x, z2, ratio=1, clims=clims, cb=false, lims=extrema(x), tick_dir=:out)
p3 = scatter([0], [0], clims=clims, zcolor=clims, grid=false,
    xlims=(1,1.1), framestyle=:none, label="", cb_title="legend")
l = @layout[grid(1,2) a{0.05w}]
plot(p1, p2, p3, layout=l, size=(1000,400))

```

 ![Plots_gr_heatmaps_single_colorbar](https://global.discourse-cdn.com/julialang/original/3X/5/0/50c988981485276557bad4949a7c700ee76357a9.png)

---

<div class="post-metadata">

**Author:** ![fredrikpaues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikpaues/32/34080_2.png) [@fredrikpaues](https://discourse.julialang.org/u/fredrikpaues)\
**Post date:** [March 2, 2022, 10:20am UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/8 "2022-03-02T10:20:28Z")

</div>

Thanks for the offer! If you could, it would be great!

---

<div class="post-metadata">

**Author:** ![fredrikpaues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikpaues/32/34080_2.png) [@fredrikpaues](https://discourse.julialang.org/u/fredrikpaues)\
**Post date:** [March 2, 2022, 10:42am UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/9 "2022-03-02T10:42:57Z")

</div>

That was great! With a few minor adjustments it results in almost exactly what I’m looking for 🙂 There is only the minor detail that the tick labels on the color bar protrude into the image’s right side… 🤔

```julia
# 2 - PLOT DATA: multiple heatmaps with single colorbar
using Plots; # No gr()
clims = extrema([z; z2])
p1 = heatmap(x, x, z, clims=clims, cb=false, lims=extrema(x), tick_dir=:out, c=:viridis) # Add c=:viridis and remove ratio=1
p2 = heatmap(x, x, z2, clims=clims, cb=false, lims=extrema(x), tick_dir=:out, c=:viridis) # Add c=:viridis and remove ratio=1
p3 = scatter([0], [0], clims=clims, zcolor=clims, grid=false,
    xlims=(1,1.1), framestyle=:none, label="", c=:viridis) # Add c=:viridis and remove cb_title="legend"
l = @layout[grid(2,1) a{0.05w}] # grid(1,2) --> grid(2,1)
plot(p1, p2, p3, layout=l)

```

![plot_30](https://global.discourse-cdn.com/julialang/original/3X/4/a/4a2dd6df3bca661a90d9542a6186ab73f1566517.png)

---

<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:** [March 2, 2022, 10:59am UTC](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/10 "2022-03-02T10:59:03Z")

</div>

> [@fredrikpaues](#):
>
> the tick labels on the color bar protrude into the image’s right side

Use Measures - see for example [here](https://discourse.julialang.org/t/plot-size-from-subplots-sizes/76542/4).
