# How to plot only the upper (or lower) triangle matrix in a heatmap?

**URL:** <https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796>\
**Category:** General Usage\
**Tags:** visualization, plots\
**Created:** [August 14, 2023, 5:58pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796 "2023-08-14T17:58:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [August 14, 2023, 5:58pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/1 "2023-08-14T17:58:19Z")

</div>

Hello, folks

I’m using a combination of functions `plot()` and `heatmap()` to visualize a correlation matrix. Correlation matrices, by definition, are symmetric, i.e., a\_{i,j} = a\_{j,i} for all i \neq j. The diagonal, where i = j, always has correlation 1 since it’s the correlation of a column with itself.

The diagonal and half of the plot is redundant or informative. For this reason, I wanted to plot only the upper (or the lower) portions of the heatmap. How can I do that?

Here’s an example of a simple heatmap. Thanks for the help.

```julia
using Distributions
using Random
using Statistics
using Plots

# Create fake data --------------------------
Random.seed!(12345)
x = Array{Float64}(undef, 500,50);

for i in 1:50 
    if i == 1
        x[:,i] = 10 .+ rand((Normal(0, 1)),500);
    else
        x[:,i] = x[:,i-1] .+ rand((Normal(0, i/2)), 500)
    end
end
correl = cor(x);

# Call heatmap -------------------------------
ht = heatmap(correl, c = cgrad([:blue,:red]));

# Plot correlation heatmap -------------------
plot(ht, yflip = true) # How to plot only the upper or lower matrix ?

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 14, 2023, 6:46pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/2 "2023-08-14T18:46:16Z")

</div>

> [@Hugo](#):
>
> The diagonal and half of the plot is redundant or informative. For this reason, I wanted to plot only the upper (or the lower) portions of the heatmap. How can I do that?

Maybe fill the other triangle with NaN values, since that typically suppresses plotting?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 14, 2023, 7:14pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/3 "2023-08-14T19:14:33Z")

</div>

```julia

function ut2ci(idx)
    j = Int(ceil(sqrt(2 * idx + 0.25) - 0.5))+1
    i = Int(idx - (j-2) * (j-1) / 2)
    CartesianIndex(i,j)
end

correl[ut2ci.(1:25*49)].=NaN
ht = heatmap(correl, c = cgrad([:blue,:red]));
plot(ht, yflip = true) 

```

no diagonal

```julia
function ut2ci(idx)
    j = Int(ceil(sqrt(2 * idx + 0.25) - 0.5))
    i = Int(idx - (j-1) * (j) / 2)
    CartesianIndex(i,j) # cartesianindex(j,i) to get the other half
end

correl[ut2ci.(1:25*51)].=NaN

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 14, 2023, 7:35pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/4 "2023-08-14T19:35:03Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `function ut2ci(idx)`

Easier to do:

```julia
correl[triu(trues(size(correl)), 1)] .= NaN # upper triangle, no diagonal

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 14, 2023, 7:38pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/5 "2023-08-14T19:38:19Z")

</div>

Another simple way:

```julia
cmap1 = cgrad([:blue,:red])
CI = CartesianIndices(correl)
ht = heatmap([(ci[1] > ci[2] ? correl[ci] : NaN) for ci in CI], c=cmap1, yflip=true)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 14, 2023, 7:49pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/6 "2023-08-14T19:49:50Z")

</div>

> [@stevengj](#):
>
> `triu(trues(size(correl)), 1)`

I’m curious to see the implementation of the triu() function.  
Using the @edit macro it sends me to the generic.jl module but I can’t find the function code

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 14, 2023, 8:11pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/7 "2023-08-14T20:11:49Z")

</div>

> <https://github.com/JuliaLang/julia/blob/master/stdlib/LinearAlgebra/src/dense.jl#L134-L145>

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 14, 2023, 8:37pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/8 "2023-08-14T20:37:13Z")

</div>

Can you also explain to me why the @edit macro sends me to genric.jl and not to dense.jl?  
or how to get to dense.jl starting from generic.jl?

---

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [August 14, 2023, 10:26pm UTC](https://discourse.julialang.org/t/how-to-plot-only-the-upper-or-lower-triangle-matrix-in-a-heatmap/102796/9 "2023-08-14T22:26:53Z")

</div>

I’m voting this one as solution because it was the easiest solution to implement and the one that relied on the most basic Julian features. All it took was the implementation of a simple `for loop` to assign `NaN` to all values I didn’t want to have in the plot.
