niltsz
niltsz

Reputation: 112

Stack Heatmaps in one plot

In Julia, I want to stack heatmaps in one plot, just how it was done with Matlab in this stackoverflow post: I need a function to display matrices stacked Preferably with the same color bar for all heatmaps, and with the ability to specify the position of each plane (i.e. along the third dimension).

I know how to stack surface plots by adding an offset to each surface (see this page: https://plotly.com/julia/3d-surface-plots/), but this is not what I want to achieve as a surface plot is not flat, but, as the name suggests, a surface. My workaround currently is to use an offset large enough that each surface appears to be flat, but as the third axis relates to the real world height of my measurements, I am not happy with this fix.

What I would prefer is a parameter positions_z = [z1, z2, z3, ...] that specifies the location of all heatmaps along the third axis, but I am also happy with workarounds. Does anyone know a solution?

Upvotes: 0

Views: 473

Answers (2)

August
August

Reputation: 12558

Here's how you can do it in Makie.jl:

using GLMakie

xs = range(-1, 1, length=10)
heights = 1:5

data = reshape(heights, 1, 1, :) .* (xs .^ 2 .+ xs' .^ 2);

fig = Figure()
ax = Axis3(fig[1, 1], aspect=(1, 1, 1), elevation=π/16)

cr = extrema(data) # color range to use for all heatmaps
for i in 1:size(data, 3)
    hm = heatmap!(ax, xs, xs, data[:, :, i], colorrange=cr)
    translate!(hm, 0, 0, heights[i])
    
    i == 1 && Colorbar(fig[1, 2], hm) # add the colorbar once
end

zlims!(ax, minimum(heights), maximum(heights))
fig

plot

Upvotes: 2

petem
petem

Reputation: 810

Here is the solution using PlotlyJS.jl. A plane is a surface, hence you can plot the heatmaps as surfaces:

using PlotlyJS

f(x,y,z) = cos(x)+cos(y)+cos(z)

n=200
xl = range(-2,2, length=n)
yl = range(-2,2, length=n)
y = yl .* ones(n)'
x = ones(n) * xl'
h = -2:1
fig = Plot()
for hp in h
    z= hp*ones(size(x))
    surfcolor = f.(x,y,z)
    addtraces!(fig, surface(x=x, y=y, z=z, coloraxis="coloraxis", surfacecolor=surfcolor));
end
relayout!(fig, font_family="Open Sherif", font_size=11, width=400, height=400, 
          margin=attr(t=10, r=10, b=10, l=4),
           coloraxis=attr(colorscale=colors.viridis,  
                          colorbar_len=0.65, 
                          colorbar_thickness=24),
          scene_camera_eye=attr(x=1.8, y=1.8, z=1))

display(fig)

enter image description here

To save your figure as a png file you should use the following settings for savefig:

savefig(fig, "parallelheatmaps.png", width=400, height=400, scale=1)

But for publication perhaps you need a pdf file with dpi=300. In this case save as:

savefig(fig, "parallelheatmaps.pdf", width=300*3, height=300*3, scale=1)

where 3 is the width and height in inches.

Upvotes: 0

Related Questions