How to make all of my heatmaps the same size?
11 views (last 30 days)
Show older comments
Hello,
I have a script that creates multiple heatmaps in a for loop, all with different amounts of data. I want all of the heatmaps to be the same size (ex. 30 x 10) even though it might only have 1 data set and fill the rest of the map with NaN values.
I created an emptymap with the NaN values of the correct size, but I'm not sure how I can pass in that empty map, so that all of the maps I create have that same size.
This is what I have so far:
%Make an empty heat map of the correct value
data = NaN(30,10);
emptyMap = heatmap(data);
%Make heat maps for each table
for i = 1:length(Tables)
figure('Name','Heatmap','NumberTitle','off');
h = heatmap(Tables{i},'C','R','ColorVariable','Code');
h.ColorLimits = [0 100];
n = 100; % Number of color steps
red = linspace(1, 0, n)'; % Red decreases from 1 to 0
green = linspace(0, 1, n)'; % Green increases from 0 to 1
blue = zeros(n, 1); % Blue stays 0
custom_map = [red green blue];
colormap(custom_map);
end
0 Comments
Accepted Answer
dpb
on 26 Mar 2025
Edited: dpb
on 26 Mar 2025
<We answered that Q? yesterday>, @Abigail, you don't need to/should not create an empty heatmap at all; you simply pad the data to the size desired and call heatmap with it.
OH! We all presumed the data were an array that just happened to be in a table...to augment the array to the fixed size, you first must create an array of the size defined by the R,C vectors and then pad it to the desired size...
nR=30; nC=10;
n = 100; % Number of color steps
r=linspace(1,0,n).'; % Red decreases from 1 to 0
g=flip(r); % Green increases from 0 to 1
b=zeros(size(r)); % Blue stays 0
custom_map=[r g b];
d=dir('example*.xlsx'); % get the list of files
for i=1:numel(d)
tT=readtable(fullfile(d(i).folder,d(i).name)); % read the file
data=zeros(max(tT.R),max(tT.C)); % initialize empty array
data(sub2ind(size(data),tT.R,tT.C))=tT.Code; % assign the row, column values
figure('Name','Heatmap','NumberTitle','off');
data=paddata(data,[nR nC],FillValue=NaN,Side="both"); % pad to the wanted size
h=heatmap(data,'Fontsize',8); % draw
colormap(custom_map);
end
"Salt to taste" with arrangement, etc., ...
6 Comments
dpb
on 26 Mar 2025
No problem, glad to help. As can observe, when don't have actual data, it's easy to make incorrect assumptions that may lead to correct solutions given those assumptions, but may not be germane to the actual problem...
More Answers (0)
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!