# How can i plot a Correlation HeatMap?

**URL:** <https://discourse.julialang.org/t/how-can-i-plot-a-correlation-heatmap/78411>\
**Category:** General Usage\
**Tags:** question, plotting, plots, statsplots\
**Created:** [March 24, 2022, 4:01pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-correlation-heatmap/78411 "2022-03-24T16:01:13Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![fabern](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@fabern](https://discourse.julialang.org/u/fabern)\
**Post date:** [October 27, 2023, 4:24pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-correlation-heatmap/78411/8 "2023-10-27T16:24:16Z")

</div>

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

```julia
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

```

 ![plot_367](https://global.discourse-cdn.com/julialang/original/3X/7/5/7557df5aa76ea731165d0181fae82bba199af78b.png)

---

_[View the full topic](https://discourse.julialang.org/t/how-can-i-plot-a-correlation-heatmap/78411)._
