Skip to content

Commit 24f1c50

Browse files
Update heatmap annotation text color logic to handle NaN values
1 parent 96e8fd9 commit 24f1c50

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

plotly/Test_plotlyfig.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,5 +2686,31 @@ function testHeatmapAxisLabels(tc)
26862686
tc.verifyEqual(p.layout.xaxis1.title.text, xLabelText);
26872687
tc.verifyEqual(p.layout.yaxis1.title.text, yLabelText);
26882688
end
2689+
2690+
function testHeatmapWithNaNValues(tc)
2691+
fig = figure("Visible","off");
2692+
data = [1 2 NaN; 4 NaN 6; 7 8 9];
2693+
heatmap(data);
2694+
2695+
p = plotlyfig(fig,"visible","off");
2696+
2697+
tc.verifyNumElements(p.data, 1);
2698+
heatmapData = p.data{1};
2699+
tc.verifyEqual(heatmapData.z, flip(data));
2700+
end
2701+
2702+
function testHeatmapAllNaNValues(tc)
2703+
fig = figure("Visible","off");
2704+
data = [NaN NaN; NaN NaN];
2705+
heatmap(data);
2706+
2707+
p = plotlyfig(fig,"visible","off");
2708+
2709+
tc.verifyNumElements(p.data, 1);
2710+
heatmapData = p.data{1};
2711+
tc.verifyEqual(heatmapData.z, flip(data));
2712+
tc.verifyTrue(all(isnan(heatmapData.z(:))));
2713+
tc.verifyTrue(all(isnan(heatmapData.text(:))));
2714+
end
26892715
end
26902716
end

plotly/plotlyfig_aux/handlegraphics/updateHeatmap.m

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@
7070
ann.x = i-1;
7171
ann.y = j-1;
7272
ann.showarrow = false;
73-
ratio = (cdata(j,i) - data.zmin) / (data.zmax - data.zmin);
74-
colorIndex = 1 + clip(round(ratio*len), 0, len);
75-
c = 255*cmap(colorIndex,:);
76-
luminance = [0.299 0.587 0.114] * c'; % ITU-R BT.601 luminance standard
77-
if luminance > 128
78-
col = [0,0,0];
79-
else
80-
col = [255,255,255];
73+
col = [255 255 255];
74+
if isfinite(cdata(j,i))
75+
ratio = (cdata(j,i) - data.zmin) / (data.zmax - data.zmin);
76+
colorIndex = 1 + clip(round(ratio*len), 0, len);
77+
c = 255*cmap(colorIndex,:);
78+
luminance = [0.299 0.587 0.114] * c'; % ITU-R BT.601 luminance standard
79+
if luminance > 128
80+
col = [0 0 0];
81+
end
8182
end
8283
ann.font.color = getStringColor(col);
8384
annotations{(i-1)*n+j} = ann;

0 commit comments

Comments
 (0)