How to change the size of heatmap axis labels
37 views (last 30 days)
Show older comments
Hello,
I'm trying to change the size of the axis labels on a heatmap, however the size the the axis labels and the tick labels remain are the same. When I change one, the other changes with it, as well as the color bar labels on the right.
I've spent a few hours looking into this but I can't find a solution. I've tried remaking the graphs using 'HeatMap', which appears to be an older version of 'heatmap', but it doesn't have the modifications I need and the results of them just look worse.
In this case of this heatmap, I'm trying to make the tick labels a size 4 font, with the axis labels a size 14.
Attached is an example file I've been using. The script is setup so that you just drag the file to your desktop to load. All you have to do is edit your user name on line 7
%% Making Heatmaps
clear
clc
% Selecting file to save, and generating file path to save
VarData=load('C:\Users\USERNAME\Desktop\ExampleFile.txt');
% prompt = 'How many hours of Dead Data? ';
% DeadDataTime = input(prompt);
DeadDataTime=4;
%% HeatMaps with all Data included
% Making 5 minute bins
% 1°PolyFit Graphs\
HMBox5=reshape(VarData, [60,((length(VarData))/60)]);
% Box5old=reshape(VarData, [60,((length(VarData))/60)]);
HMBox5=HMBox5(:);
Livebin5min=reshape(HMBox5(1:(length(HMBox5)-(DeadDataTime*12*60))), 60, []);
Deadbin5min=reshape(HMBox5(1+((length(HMBox5))-(DeadDataTime*12*60)):length(HMBox5)), 60, []);
% Generating X Axis
XaxisHourCounter=0;
Xaxis=string(1:1:((length(HMBox5)-(DeadDataTime*12*60))/60));
for i=1:length(Xaxis);
count_i=num2str(round(XaxisHourCounter, 2), '%4.2f');
count_f=num2str(round(XaxisHourCounter+(1/12), 2), '%4.2f');
XaxisNum=strcat(count_i, '-', count_f);
Xaxis(i)=(XaxisNum);
XaxisHourCounter=XaxisHourCounter+(1/12);
end
% Generating Y Axis
YaxisHourCounter=((((length(HMBox5))-(DeadDataTime*12*60))/60)/12);
Yaxis=string((((length(HMBox5))-(DeadDataTime*12*60))/60):1:((length(HMBox5)/60))-1);
for i=1:length(Yaxis);
count_i=num2str(round(YaxisHourCounter, 2), '%4.2f');
count_f=num2str(round(YaxisHourCounter+(1/12), 2), '%4.2f');
YaxisNum=strcat(count_i, '-', count_f);
Yaxis(i)=(YaxisNum);
YaxisHourCounter=YaxisHourCounter+(1/12);
end
% Creating X and Y labels
% Alocattign space for Table
Tmatrix=zeros((DeadDataTime*12),(((length(HMBox5))-(DeadDataTime*12*60))/60));
% Filling Table
for Deadi=1:(DeadDataTime*12);
for Livei=1:(((length(HMBox5))-(DeadDataTime*12*60))/60);
LiveCol=Livebin5min(:,Livei);
DeadCol=Deadbin5min(:,Deadi);
[h, p]=ttest(DeadCol, LiveCol);
Tmatrix(Deadi, Livei)=p;
end
end
% Saving P-Value data in Table format. Not too much effort put into this.
% MIssing X-axis, but lines up with heatmap. Use heatmap as reference for
% layout. Saved in Tab-Deliminate format.
% writematrix(Tmatrix, strcat(HeatmapPathSave, '\', '5minbin10dsMatrix'), 'Delimiter','tab');
% Removing P Values.
Tmatrix(Tmatrix >= 0.05) = NaN;
% Import Table to heatmap
H=heatmap(Xaxis, Yaxis, Tmatrix, 'Colormap', parula(20), 'MissingDataColor', 'w', 'MissingDataLabel','P > 0.05', 'ColorScaling', 'log', 'YLabel', 'Dead Data', 'FontSize', 4, 'XLabel', 'Live Data');
caxis([ log(0.00001) log(0.05)]);
ax = gca;
% ax.FontSize = 4;
x0=10;
y0=10;
width=2000;
height=38.6363+((DeadDataTime/3)*261.3636); %These weird numbers are chosen for the length of the X Label which is fixed (77.27), while the chart is variable (222.72). Normalized using 300 as a baseline.
set(gcf,'position',[x0,y0,width,height]);
0 Comments
Answers (2)
Adam Danz
on 2 Aug 2024
How to independently change fontsize of tick labels and axis labels in heatmap
Heatmap's FontSize property sets the font size for the entire heatmap chart. Same with using the fontsize(h,20,'points') command where h is a HeatmapChart object. The axes and tick labels do not have a font size property.
To set the font size of the axis labels only, use tex markup (LaTeX also supported). Append \fontsize{_} to the labels after filling in the font size. An easy way to update existing labels is to use strcat to concatenate the tex markup command with the existing strings.
The example sets the xlabel font size to 20 and the ylabel font size to 18.
T = readtable('outages.csv');
h = heatmap(T,'Region','Cause');
h.XLabel = strcat('\fontsize{20}',h.XLabel);
h.YLabel = strcat('\fontsize{18}',h.YLabel);
To set the font size of tick labels only, use the same approach.
h.XDisplayLabels = strcat('\fontsize{15}',h.XDisplayLabels);
h.YDisplayLabels = strcat('\fontsize{12}',h.YDisplayLabels);
0 Comments
Ryan Verburg
on 31 Oct 2024 at 15:00
Hey Adam Danz,
I am currently looking for a way to change the font of the column and row labels; that is, in your example, MidWest, NorthEast, etc. Do you happen to know how to do this?
Thanks in advance.
0 Comments
See Also
Categories
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!