How can i plot a Correlation HeatMap?

If somebody arrives here looking for a solution with Makie. Below is an adaptation of @rafael.guerra’s code:

df = DataFrame(Age=rand(4), Balance=rand(4), Tenure=rand(4), Salary=rand(4))
cols = [:Age, :Balance, :Tenure]  # define subset
M = cor(Matrix(df[!,cols]))       # correlation matrix
(n,m) = size(M)
# # Plots.jl
# Plots.heatmap(M, fc=cgrad([:white,:dodgerblue4]), xticks=(1:m,cols), xrot=90, yticks=(1:m,cols), yflip=true)
# Plots.annotate!([(j, i, Plots.text(round(M[i,j],digits=3), 8,"Computer Modern",:black)) for i in 1:n for j in 1:m])
# Makie.jl
fig, axis_hm, plot_hm =
    Makie.heatmap(M;
        colormap = :RdBu, colorrange = (-1,1),
        axis = (xticks=(1:m, String.(cols)),
                yticks=(1:m,String.(cols)),
                yreversed=true,
                xticklabelrotation = π/2));
Makie.Colorbar(fig[1, 2], plot_hm);
[Makie.text!(axis_hm,
    "$(round(M[i,j],digits=3))",
    position = (i,j),
    align = (:center, :center), fontsize=14,
    color = ifelse(abs(M[i,j]) > 0.5, :white, :black))
    for i in 1:n for j in 1:m];
fig

3 Likes