Skip to content

Commit 8042b6d

Browse files
Fix missing heatmap cell annotations, only add heatmap title annotation if there is a title
1 parent d6655b8 commit 8042b6d

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

plotly/Test_plotlyfig.m

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,9 +2589,9 @@ function testHeatmapTextColorBrightCells(tc)
25892589

25902590
p = plotlyfig(fig,"visible","off");
25912591

2592-
tc.verifyNumElements(p.layout.annotations, numel(data)+1);
2592+
tc.verifyNumElements(p.layout.annotations, numel(data));
25932593
% All cells should have black text due to bright background
2594-
for i = 2:numel(p.layout.annotations)-1
2594+
for i = 1:numel(p.layout.annotations)
25952595
ann = p.layout.annotations{i};
25962596
tc.verifyEqual(ann.font.color, "rgb(0,0,0)"); % Black text
25972597
end
@@ -2605,12 +2605,39 @@ function testHeatmapTextColorDarkCells(tc)
26052605

26062606
p = plotlyfig(fig,"visible","off");
26072607

2608-
tc.verifyNumElements(p.layout.annotations, numel(data)+1);
2608+
tc.verifyNumElements(p.layout.annotations, numel(data));
26092609
% All cells should have white text due to dark background
2610-
for i = 2:numel(p.layout.annotations)-1
2610+
for i = 1:numel(p.layout.annotations)
26112611
ann = p.layout.annotations{i};
26122612
tc.verifyEqual(ann.font.color, "rgb(255,255,255)"); % White text
26132613
end
26142614
end
2615+
2616+
function testHeatmapCellTextAnnotations(tc)
2617+
fig = figure("Visible","off");
2618+
data = [1 2; 3 4];
2619+
heatmap(data);
2620+
2621+
p = plotlyfig(fig,"visible","off");
2622+
2623+
tc.verifyNumElements(p.layout.annotations, numel(data));
2624+
actualStrings = cellfun(@(ann) ann.text, p.layout.annotations);
2625+
expectedStrings = arrayfun(@(v) string(v), data(:));
2626+
tc.verifyEqual(sort(actualStrings(:)), sort(expectedStrings(:)));
2627+
end
2628+
2629+
function testHeatmapTitleAnnotation(tc)
2630+
fig = figure("Visible","off");
2631+
data = [1 2; 3 4];
2632+
titleString = "title";
2633+
heatmap(data);
2634+
title(titleString);
2635+
2636+
p = plotlyfig(fig,"visible","off");
2637+
2638+
tc.verifyNumElements(p.layout.annotations, numel(data)+1);
2639+
actualStrings = cellfun(@(ann) ann.text, p.layout.annotations);
2640+
tc.verifyNotEmpty(actualStrings(contains(actualStrings, titleString)));
2641+
end
26152642
end
26162643
end

plotly/plotlyfig.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,11 @@ function validate(obj)
629629
for n = 1:obj.State.Figure.NumTexts
630630
try
631631
if obj.PlotOptions.is_headmap_axis
632-
updateHeatmapAnnotation(obj,n);
632+
if ~isempty(obj.State.Text(n).Handle)
633+
nanns = length(obj.layout.annotations);
634+
axIndex = nanns + obj.getAxisIndex(obj.State.Text(n).AssociatedAxis);
635+
obj.layout.annotations{axIndex} = getHeatmapTitleAnnotation(obj,n);
636+
end
633637
obj.PlotOptions.CleanFeedTitle = false;
634638
elseif obj.PlotlyDefaults.isGeoaxis
635639
% TODO

plotly/plotlyfig_aux/core/updateHeatmapAnnotation.m renamed to plotly/plotlyfig_aux/core/getHeatmapTitleAnnotation.m

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function obj = updateHeatmapAnnotation(obj,anIndex)
1+
function ann = getHeatmapTitleAnnotation(obj,anIndex)
22
%-------X/YLABEL FIELDS--------%
33
% title...[DONE]
44
% titlefont.size...[DONE]
@@ -29,42 +29,35 @@
2929
% bgcolor: ...[DONE]
3030
% opacity: ...[NOT SUPPORTED IN MATLAB]
3131

32-
%-AXIS INDEX-%
33-
nanns = length(obj.layout.annotations);
34-
axIndex = nanns + obj.getAxisIndex(obj.State.Text(anIndex).AssociatedAxis);
35-
36-
%-CHECK FOR MULTIPLE AXES-%
3732
[xsource, ysource] = findSourceAxis(obj,anIndex);
3833

39-
%-get heatmap title name-%
4034
title_name = obj.State.Text(anIndex).Handle;
41-
42-
obj.layout.annotations{axIndex}.showarrow = false;
35+
ann.showarrow = false;
4336

4437
%-anchor title to paper-%
4538
if obj.State.Text(anIndex).Title
46-
obj.layout.annotations{axIndex}.xref = "paper";
47-
obj.layout.annotations{axIndex}.yref = "paper";
39+
ann.xref = "paper";
40+
ann.yref = "paper";
4841
else
49-
obj.layout.annotations{axIndex}.xref = "x" + xsource;
50-
obj.layout.annotations{axIndex}.yref = "y" + ysource;
42+
ann.xref = "x" + xsource;
43+
ann.yref = "y" + ysource;
5144
end
5245

53-
obj.layout.annotations{axIndex}.xanchor = "middle";
54-
obj.layout.annotations{axIndex}.align = "middle";
55-
obj.layout.annotations{axIndex}.yanchor = "top";
56-
obj.layout.annotations{axIndex}.text = sprintf("<b>%s</b>", title_name);
57-
obj.layout.annotations{axIndex}.font.size = 14;
46+
ann.xanchor = "middle";
47+
ann.align = "middle";
48+
ann.yanchor = "top";
49+
ann.text = sprintf("<b>%s</b>", title_name);
50+
ann.font.size = 14;
5851

5952
if obj.State.Text(anIndex).Title
6053
%-AXIS DATA-%
6154
xaxis = obj.layout.("xaxis" + xsource);
6255
yaxis = obj.layout.("yaxis" + ysource);
6356

64-
obj.layout.annotations{axIndex}.x = mean(xaxis.domain);
65-
obj.layout.annotations{axIndex}.y = (yaxis.domain(2) + 0.04);
57+
ann.x = mean(xaxis.domain);
58+
ann.y = (yaxis.domain(2) + 0.04);
6659
else
67-
obj.layout.annotations{axIndex}.x = text_data.Position(1);
68-
obj.layout.annotations{axIndex}.y = text_data.Position(2);
60+
ann.x = text_data.Position(1);
61+
ann.y = text_data.Position(2);
6962
end
7063
end

plotly/plotlyfig_aux/handlegraphics/updateHeatmap.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
values = heat_data.ColorDisplayData';
6969
for i = 1:m
7070
for j = 1:n
71-
ann.text = num2str(round(cdata(j,i), 2));
71+
ann.text = string(num2str(round(cdata(j,i), 2)));
7272
ann.x = i-1;
7373
ann.y = j-1;
7474
ann.showarrow = false;
@@ -81,7 +81,7 @@
8181
col = [255,255,255];
8282
end
8383
ann.font.color = getStringColor(col);
84-
annotations{i*(m-1)+j} = ann;
84+
annotations{(i-1)*n+j} = ann;
8585
end
8686
end
8787

0 commit comments

Comments
 (0)