Heatmap doesnt show empty rows or columns

14 views (last 30 days)
I have binned Data, one time vector and one data vector.
If I want to make a heatmap of this data, where i can see the instances of each bin over 24 hours. In my example, a bin is 2 hours on the x axis.
The heatmap doesnt plot rows or columns where no data is available, as seen below. The Rows 10 and twelve are missing.
Here is my code:
n_bins = 12;
t = table(Var1Discrete,Var2Discrete,'VariableNames',[Var1Name, Var2Name]);
hm = heatmap(t,Var1Name, Var2Name);
hm.MissingDataColor = [0,0,0];
hm.MissingDataLabel = 'NaN';
hm.GridVisible = false;
hm.XDisplayLabels = E1(1:n_bins);
hm.YDisplayLabels = E2(1:n_bins);
hm.ColorScaling = ColorScaling;
matlab also ignores the MissingDataColor and -Label.
As Rows are missing, it doesnt label the Y-Data, but numbers the different bins. I get the error:
Error using matlab.graphics.chart.HeatmapChart/set.YDisplayLabels
'YDisplayLabels' vector must contain the same number of elements as the 'YDisplayData' vector
How can I fix this?

Answers (2)

Adam Danz
Adam Danz on 14 Nov 2022
By empty I assume you mean that there are NaN values present and I see that you are setting a missing data value color and label which would be consistent with that assumption.
Heatmap does indicate NaNs.
x = magic(12);
x(:,4) = nan;
x(4,:) = nan;
The error you're seeing is likely due to an indexing problem in
hm.YDisplayLabels = E2(1:n_bins);
But since we do not have access to E2 and n_bins, I can't be more specific.
  1 Comment
Johannes Haßler
Johannes Haßler on 15 Nov 2022
Edited: Johannes Haßler on 15 Nov 2022
Hi Adam, thank you for your reply!
n_bins is in the code above, the number of bins used (e.g. 12)
E1 and E2 is an Array of range end values to specify the bucket. in the example E1 is [00:00, 01:00, 02:00, ... 23:00, 24:00]. As 00:00 and 24:00 are the same i implemented it in such a way, that E1 has n_bins+1 indexes.
And to be consistent I implemented it the same way with E2.
The thing is, that Var2Discrete has no indexes in the bins 10 and 12.
shows, that 10 and 12 are missing from this array.
Your example delivers the same result as you posted. So i have to investigate whats going on in my code.

Sign in to comment.

Johannes Haßler
Johannes Haßler on 15 Nov 2022
I worked around the problem by creating a value matrix by myself:
matrix = zeros(n_bins,n_bins);
for i = 1 : n_bins
for j = 1 : n_bins
n = 0;
for k = 1:length(Var1)
if j == Var1Discrete(k) && i == Var2Discrete(k)
n = n + 1;
matrix(i,j) = n;
hm = heatmap(matrix);


Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by