How can I save a Makie heatmap as PNG with one pixel per matrix element?

For example:

using Makie
x = rand(300, 400)
fig = heatmap(x)
Makie.save("out1.png", fig)  # 800 x 600
Makie.save("out2.png", fig, px_per_unit=1)  # 800 x 600
Makie.save("out3.png", fig, resolution=size(x))  # 300 x 400

The last version (out3.png) is closest to what I want, but it squeezes the actual heatmap and the x & y axes into a png of size 300 x 400. I want the plotting area to be 300 x 400 (so the saved image with axes becomes slightly larger), i.e. using exactly one pixel per matrix element. Is this possible?

You can do this by directly drawing to a scene with the pixel camera projection:

x = rand(300, 400)
scene = Scene(camera=campixel!, resolution=size(x))
heatmap!(scene, x)
save("out.png", scene)
4 Likes