logo

How to output and save arrays as heatmap images in Julia 📂Julia

How to output and save arrays as heatmap images in Julia

Heatmap

Using the heatmap function from the Plots package, you can output a 2D array as a heatmap image, and with the savefig function, you can save the resulting image. The @__DIR__ macro tells you the location of the Julia code file.

# code1

heatmap1.png

However, if you compare array A with the heatmap image, you may notice that the top and bottom of the array are flipped in the heatmap image. The reason the output image is created this way is official crab talk says it’s because each element’s position is thought of not in terms of rows and columns but rather as coordinates in a Cartesian coordinate system. That is, for example, in the matrix $A$, the value 19 is not seen as an element in the 4th row and 4th column but as an element in Cartesian coordinates $(4,4)$. This explains why the matrix and the image are flipped upside down with respect to each other.

Therefore, to make the output look the same as the array, you can add the yflip=true option1.

# code2

heatmap2.png

Moreover, users familiar with MATLAB can use the color=:bgy option to get an output that closely matches MATLAB’s default colors.

# code3

heatmap3.png

Color Themes

The following are some of the available color themes.

3.png

2.png

See Also

Environment

  • OS: Windows10
  • Version: 1.5.3 (2020-11-09)