# Heatmap get scrambled

**URL:** <https://discourse.julialang.org/t/heatmap-get-scrambled/77292>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [March 2, 2022, 11:52am UTC](https://discourse.julialang.org/t/heatmap-get-scrambled/77292 "2022-03-02T11:52:22Z")\
**Posts on this page:** 6\
**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 2, 2022, 11:52am UTC](https://discourse.julialang.org/t/heatmap-get-scrambled/77292/1 "2022-03-02T11:52:22Z")

</div>

When I try to implement [a solution from my last question](https://discourse.julialang.org/t/heatmap-with-irregularly-spaced-grids/77259/7), I run into new problems: `p1` and `p2` (generated below) result in two completely different plots. I expected that they would look very similar except for the fact that `p1` would account for the non-linear spacing of the `x` and `y` grids while `p2` would just uses the indices of the grids. `p2` looks as expected but `p1` looks like it has been run in a blender 😭

```julia
# 1 - INPUT DATA: generate non-linear grids 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
n2 = 30
y = zeros(n2)
for i in 2:n2
    y[i] = y[i-1] + (2 - y[i-1]) / (n2-i+1)^1.3
end
r = @. x^2 + y'^2
z = @. sin(10*r) / (1 + r)

using Plots
p1 = heatmap(
    x, y, z;
    clims=clims,
    cb=false,
    xlims=extrema(x),
    ylims=extrema(y),
    tick_dir=:out,
    c=:viridis
)
p2 = heatmap(
    z;
    clims=clims,
    cb=false,
    tick_dir=:out,
    c=:viridis
)

```

![p1](https://global.discourse-cdn.com/julialang/original/3X/1/8/18426eb5b6d23401c7c0e634cebfaf400fde85cc.png)  
 ![p2](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2a8be58448162e2f8ab80bf729861c4b5b2945a.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, 12:07pm UTC](https://discourse.julialang.org/t/heatmap-get-scrambled/77292/2 "2022-03-02T12:07:05Z")

</div>

> [@fredrikpaues](#):
>
> ```julia
> using Plots
> p1 = heatmap(
> x, y, z;
> 
> ```

You seem to have mixed rows and colums, try the transpose:

```julia
p1 = heatmap(
    x, y, z';

```

---

<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, 2:06pm UTC](https://discourse.julialang.org/t/heatmap-get-scrambled/77292/3 "2022-03-02T14:06:38Z")

</div>

But the logic of that defies me. I would have thought that `size(z) == (size(x, 1), size(y, 1))` would imply that inputting simply `x, y, z` would get the job done 🤯

---

<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, 2:24pm UTC](https://discourse.julialang.org/t/heatmap-get-scrambled/77292/4 "2022-03-02T14:24:22Z")

</div>

Julia is column-major and `size(z) = (20,30)` tells us that the first index of matrix `z` runs over 1:20 vertically along the columns direction. If you want `x` to display horizontally you have to transpose `z`

---

<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, 2:39pm UTC](https://discourse.julialang.org/t/heatmap-get-scrambled/77292/5 "2022-03-02T14:39:44Z")

</div>

Alright. I just realized that `p2` didn’t look as I should have expected. It has 30 nodes on the horizontal axis and 20 on the vertical, which makes perfect sense after your comment. Thanks!

Now, the strange thing becomes why `heatmap` doesn’t object when the supplied grids have the wrong number of nodes. Perhaps there is some deep reason for this 🙂 The correct call when `z` wasn’t transposed should have been `heatmap(y, x, z)`.

---

<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, 3:50pm UTC](https://discourse.julialang.org/t/heatmap-get-scrambled/77292/6 "2022-03-02T15:50:55Z")

</div>

> [@fredrikpaues](#):
>
> the strange thing becomes why `heatmap` doesn’t object when the supplied grids have the wrong number of nodes

Indeed this is confusing and I’ve no idea about the rationale behind it.

There should be somewhere a `reshape()` being applied, as `heatmap(x,y,z)` seems to consume any matrix `z` with 600 elements (= 20 x 30) in your example, for instance `z = rand(10,60)`
