# How can I make this correlation plot?

**URL:** <https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 16, 2023, 7:16am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434 "2023-06-16T07:16:35Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 16, 2023, 7:16am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/1 "2023-06-16T07:16:35Z")

</div>

how can I make a plot like this in Julia? Which package should i use? I have used corrplot but I don’t think we can get plot like this with that.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/0/10091e86599cafed5472f66a212be0576ec622e6.jpeg)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 16, 2023, 7:29am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/2 "2023-06-16T07:29:27Z")

</div>

Isn’t that just a `heatmap` with a bunch of annotations? Shouldn’t be very hard in any plotting package?

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 16, 2023, 7:33am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/3 "2023-06-16T07:33:34Z")

</div>

Ok I will try that :).

A follow up what about this plot with base function? with correlation coefficient.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/e/7e911fa210b81085cd51f78022b9a047bd586db2.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 16, 2023, 7:39am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/4 "2023-06-16T07:39:24Z")

</div>

That’s basically `corrplot` from `StatsPlots` though isn’t it?

![](https://global.discourse-cdn.com/julialang/original/3X/a/9/a98b680881755f9e8821f6f9d90d3df9ecbe983c.png)  
If you want to exactly reproduce some R plot, you can also just RCall ggplot2 (or whatever you’re using in R). Alternatively you’ll have to get comfortable with the details of composing plots in your favourite plotting library (e.g. your last plot looks like three `density` plots, three `scatter` plots and three emtpy plots with just `annotate` in Plots.jl, but getting the legends to look exactly like you have there as grey boxes might be a bit of effort and potentially easier in Makie)

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 16, 2023, 7:41am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/5 "2023-06-16T07:41:44Z")

</div>

In corrplot is there any option to remove right side plots and rather get corr values ?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 16, 2023, 8:05am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/6 "2023-06-16T08:05:19Z")

</div>

Not that I know of, but look at the docs. As I said if you want a plot to exactly look a specific way you should just build it, it’s pretty simple to stitch together from the basics and you can customise things to your heart’s content.

---

<div class="post-metadata">

**Author:** ![Diego\_Javier\_Zea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diego_javier_zea/32/1858_2.png) [@Diego\_Javier\_Zea](https://discourse.julialang.org/u/Diego_Javier_Zea)\
**Post date:** [June 16, 2023, 9:07am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/7 "2023-06-16T09:07:48Z")

</div>

Hi, I think you are looking for this: [How can i plot a Correlation HeatMap? - #6 by rafael.guerra](https://discourse.julialang.org/t/how-can-i-plot-a-correlation-heatmap/78411/6) Cheers

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 16, 2023, 9:09am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/8 "2023-06-16T09:09:43Z")

</div>

Yeah I found that after sometime 😅. Thanks!

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 16, 2023, 2:02pm UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/9 "2023-06-16T14:02:21Z")

</div>

when i’m saving my plot, I’m getting blur image for right side plots. Also I had question what exactly right side represent?

![image](https://global.discourse-cdn.com/julialang/original/3X/8/3/83debf417245b7b43ee82472da932b87a1940410.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 16, 2023, 6:12pm UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/10 "2023-06-16T18:12:05Z")

</div>

People have replied to quite a few of your threads already that asking for help without any runnable code is hard, but for completeness please read

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

And follow those best practices when posting.

---

<div class="post-metadata">

**Author:** ![indymnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/indymnv/32/212594_2.png) [@indymnv](https://discourse.julialang.org/u/indymnv)\
**Post date:** [June 18, 2023, 4:33am UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/11 "2023-06-18T04:33:41Z")

</div>

I did that plot sometime ago with Plots and DataFrames and corr, I wrote an small article here [World Happiness Report - EDA & clustering with Julia - Julia Community 🟣](https://forem.julialang.org/navi/world-happiness-report-eda-clustering-with-julia-1b21)

You can use a function like this:

```julia
function heatmap_cor(df)
    cm = cor(Matrix(df))
    cols = Symbol.(names(df))

    (n,m) = size(cm)
    display(
    heatmap(cm, 
        fc = cgrad([:white,:dodgerblue4]),
        xticks = (1:m,cols),
        xrot= 90,
        size= (800, 800),
        yticks = (1:m,cols),
        yflip=true))
    display(
    annotate!([(j, i, text(round(cm[i,j],digits=3),
                       8,"Computer Modern",:black))
           for i in 1:n for j in 1:m])
    )
end

```

In that exercise the result was as follow:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/6/06956ed5650c4282ded14cd178fd3980e8bc2b86.jpeg)

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [June 18, 2023, 5:49pm UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/12 "2023-06-18T17:49:34Z")

</div>

This sort of thing is called a scatter-plot matrix, corner plot, or pair plot. There is [a Julia package called `PairPlots.jl`](https://github.com/sefffal/PairPlots.jl) that does this for you, except for the correlation values. But you could presumably add those pretty easily using [Makie’s `text` function](https://docs.makie.org/stable/examples/plotting_functions/text/index.html#text).

---

<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:33pm UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/13 "2023-10-27T16:33:15Z")

</div>

Others have looked for this as well. This takes a few lines of code to implement yourself. Have a look at [How can i plot a Correlation HeatMap? - #8 by fabern](https://discourse.julialang.org/t/how-can-i-plot-a-correlation-heatmap/78411/8), that shows solutions with `Plots.jl` or `Makie.jl`.

---

<div class="post-metadata">

**Author:** ![dantebertuzzi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dantebertuzzi/32/215393_2.png) [@dantebertuzzi](https://discourse.julialang.org/u/dantebertuzzi)\
**Post date:** [February 21, 2025, 10:24pm UTC](https://discourse.julialang.org/t/how-can-i-make-this-correlation-plot/100434/14 "2025-02-21T22:24:58Z")

</div>

```julia
theme(:dracula)
# Calcular matriz de correlação
cor_matrix = cor(Matrix(df_clean))
n_vars = size(cor_matrix, 1)
var_names = names(df_clean)

# Criar máscara triangular inferior
mask = [i >= j for i in 1:n_vars, j in 1:n_vars]

# Aplicar máscara: valores fora do triângulo ficam como NaN
masked_cor = cor_matrix .* mask
masked_cor[.!mask] .= NaN

# Inverter as linhas da matriz para alinhar com os nomes invertidos no eixo Y
reversed_masked_cor = reverse(masked_cor, dims=1)
reversed_names = reverse(var_names)

# Criar heatmap
plt = heatmap(
    var_names, reversed_names, # Eixos com nomes originais e invertidos
    reversed_masked_cor, # Matriz invertida verticalmente
    color = :RdBu, clims = (-1, 1),
    xrot = 45, size = (600, 500),
    colorbar_title = "",
    title = "Matriz de Correlação"
)

# Adicionar anotações nas posições corretas (considerando a inversão vertical)
for i in 1:n_vars
    for j in 1:i # Apenas triângulo inferior
        value = round(cor_matrix[i, j], digits=3)
        text_color = abs(value) > 0.5 ? :white : :black
        # Posição X: coluna j, Posição Y: linha ajustada pela inversão
        annotate!(plt, j - 0.5, n_vars - i + 0.5, text("$value", 8, text_color, :center))
    end
end

display(plt)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/8/98d5ff1fb5ebf7520ad7c19d030efa50be599775.png)
