Why this code can't annote all heatmap cells?

Hello, good morning.
I’m trying o annotate a heatmap, but the labels are going wrong in the map as the figure below shows
May someone help?

image

source

using Plots

week_day = randn(24, 5);

xlabel = ["Mon", "Tue", "Wed", "Thu", "Fri"]

heatmap(week_day, xticks=(1:5, xlabel), yticks=(1:24), fill_z=week_day)

fontsize = 5

nrow, ncol = size(week_day)

ann = [(i, j, text(round(week_day[i, j], digits=2), fontsize, :white, :center)) for i in 1:nrow for j in 1:ncol]

annotate!(ann, linecolor=:white)

It looks like i and j are swapped.
Try:

ann = [(j, i, text(round(week_day[i, j], digits=2), fontsize, :white, :center)) for i in 1:nrow for j in 1:ncol]
1 Like

It works, but I didn’t get the issue. “I” is row and “j” is column.
Why here we are indexing with:

 [(j, i...

2d plots have the first index along the x-axis and second index along y-axis. That is how annotate interprets them. However, Julia matrices (and as plotted by heatmap) have their first index along the “vertical y-axis” and the second index along the “horizontal x-axis”. They are swapped.

1 Like

Yep. Inderstood. Thats why the tuple needs to be swapped. Very smart :clap: :clap: