How to make all of my heatmaps the same size?

조회 수: 12 (최근 30일)
KD
KD 2025년 3월 26일
댓글: dpb 2025년 3월 26일
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

채택된 답변

dpb
dpb 2025년 3월 26일
편집: dpb 2025년 3월 26일
<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
KD
KD 2025년 3월 26일
@dpb thank you so much!!!! this is exactly what I needed to do. Thank you for bearing with me with getting all the right data together, I really appreciate it!
dpb
dpb 2025년 3월 26일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by