Heatmap get scrambled

When I try to implement a solution from my last question, 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 :sob:

# 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
p2

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

p1 = heatmap(
    x, y, z';
1 Like

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 :exploding_head:

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

1 Like

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 :slight_smile: The correct call when z wasn’t transposed should have been heatmap(y, x, z).

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)

1 Like