Heatmap doesnt show empty rows or columns

조회 수: 19 (최근 30일)
Johannes Haßler
Johannes Haßler 2022년 11월 14일
답변: Johannes Haßler 2022년 11월 15일
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?

답변 (2개)

Adam Danz
Adam Danz 2022년 11월 14일
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;
figure()
heatmap(x)
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
Johannes Haßler
Johannes Haßler 2022년 11월 15일
편집: Johannes Haßler 2022년 11월 15일
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.
unique(Var2Discrete)
shows, that 10 and 12 are missing from this array.
edit:
Your example delivers the same result as you posted. So i have to investigate whats going on in my code.

댓글을 달려면 로그인하십시오.


Johannes Haßler
Johannes Haßler 2022년 11월 15일
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;
end
end
matrix(i,j) = n;
end
end
hm = heatmap(matrix);
...

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by