How can I italicize the label of a heatmap?
15 views (last 30 days)
Show older comments
I just can not italicize the colume labels using the "\it" command. How can I fix it?
figure
xvalues=linspace(1,8,8);
yvalues={"\it Campylobacter (gull,+)","{\it Salmonella} (gull,+)", "nororvirus (sewage,+)", "adenovirus (sewage,-)", ...
"{\it Campylobacter} (sewage,+)","{\it Salmonella} (sewage,+)","{\it E.coli} O157:H7 (sewage,+)","{\it Cryptosporidium} (sewage,+)",...
"{\it Giardia} (sewage,+)", "norovirus (effluent,+)", "adenovirus (effluent,-)","{\it Cryptosporidium} (effluent,+)",...
"{\it Giardia} (effluent,+)","{\it Campylobacter} (cattle,+)","{\it Salmonella} (cattle,+)","{\it E. coli} O157:H7 (cattle,+)",...
"{\it Cryptosporidium} (cattle,+)","{\it Giardia} (cattle,+)"};
htmap=heatmap(xvalues,yvalues,mprtable);
htmap.Colormap=redblue;
set(gca,'XLabel','GeM concentration (GC/100 mL)','YLabel','R_p');
0 Comments
Accepted Answer
Benjamin Kraus
on 20 Sep 2023
Edited: Benjamin Kraus
on 20 Sep 2023
Heatmap does allow you to customize the X and Y tick labels, by setting the XData and YData properties (alternatively, you can set the XDisplayLabels or YDisplayLabels properties).
Since R2019a the default interpreter for heatmap is set to 'tex' so you can use special characters (such as \it) to create italic text. Prior to R2019a, if you wanted to use 'tex' or 'latex' interpretted characters, you can use the answer by @Adam Danz (click here).
Starting in R2023b, you can also change the interpreter used by setting the new Interpreter property.
xvalues=linspace(1,8,8);
yvalues={"\it Campylobacter (gull,+)","{\it Salmonella} (gull,+)", "nororvirus (sewage,+)", "adenovirus (sewage,-)", ...
"{\it Campylobacter} (sewage,+)","{\it Salmonella} (sewage,+)","{\it E.coli} O157:H7 (sewage,+)","{\it Cryptosporidium} (sewage,+)",...
"{\it Giardia} (sewage,+)", "norovirus (effluent,+)", "adenovirus (effluent,-)","{\it Cryptosporidium} (effluent,+)",...
"{\it Giardia} (effluent,+)","{\it Campylobacter} (cattle,+)","{\it Salmonella} (cattle,+)","{\it E. coli} O157:H7 (cattle,+)",...
"{\it Cryptosporidium} (cattle,+)","{\it Giardia} (cattle,+)"};
htmap=heatmap(xvalues,yvalues,rand(18,8));
htmap.XLabel = 'GeM concentration (GC/100 mL)';
htmap.YLabel = 'R_p';
htmap.XData = xvalues;
htmap.YData = yvalues;
0 Comments
More Answers (1)
Adam Danz
on 29 Nov 2018
Edited: Adam Danz
on 3 Jan 2020
The heatmap() function (released 2017a) unfortunately doesn't provide handles to the column and row labels so there's no way of changing their 'interpreter' so that the italics is recognized. I don't think it's even possible to get access to those handles using methods such as findall() (I tried).
The older HeatMap() function (released 2009b, Bioinformatics toolbox) is more customizable. For example, the addYLabel() function allows users to customize the row labels but it only works with the older version of HeatMap().
If you really need to make that change, one hacky method would be to remove the row labels, put a 2nd transparent axes over top of the heatmap, calculate where each row-center is, and then set the yticks and ylabels.
h = heatmap(xvalues,yvalues,cdata);
h.YDisplayLabels = repmat({''}, size(h.YData)); %remove row labels
a2 = axes('Position', h.Position); %new axis on top
a2.Color = 'none'; %new axis transparent
a2.YTick = 1:size(h.ColorData,1); %set y ticks to number of rows
a2.XTick = []; %Remove xtick
% alternatively, to preserve axis label spacing :
% a2.XTickLabel = repmat({' '}, size(a2.XTick));
ylim(a2, [0.5, size(h.ColorData,1)+.5]) %center the tick marks
a2.YDir = 'Reverse'; %flip your y axis to correspond with heatmap's
a2.YTickLabel = {'\it Green','\it Red',...}; %set you ytick labels here, formatted.
7 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!