How to make all of my heatmaps the same size?

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

 Réponse acceptée

dpb
dpb le 26 Mar 2025
Modifié(e) : dpb le 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 commentaires

KD
KD le 26 Mar 2025
Hello,
I already tried this, but it doesn't give me the result I was aiming for. That is why I tried asking the question in a different way.
When I call data with the heatmap, data only contains one column of my whole data set, so it doesn't recognize the properties from my chart and then my map no longer is an accurate representation of my data.
The rows and columns of my heatmap correspond to the data set.
For example row 5, col 10 = 50
Im not super familiar with paddata, but is there a way to use it where I don't have to set my table to my variable name? Its when I do this that I lose the rest of my data.
Thanks for your help so far!
dpb
dpb le 26 Mar 2025
Modifié(e) : dpb le 26 Mar 2025
It's not possible to answer without seeing the data -- attach a representative data file.
Is or is not the result before that you accepted what you're looking for? If so, the above will work if implemented correctly with the actual data; if not, we need to know what you are expecting.
You never set the table to anything; you reference the data array within the table. The problem is, we can't see what the table contains...
KD
KD le 26 Mar 2025
I uploaded 3 examples of what data would be in my tables. From these tables, I'll create 3 heatmaps with the Code in the corresponding R/C.
I want all of the heatmaps to be the same amount of rows and columns, even if it doesn't have data to fill them up.
The answer I accepted previously helped me make the empty heatmap that I thought would help me accomplish this goal.
Steven Lord
Steven Lord le 26 Mar 2025
Please show us the exact code you use to read in one of those spreadsheets (do you read it into a table array, do you read it into a numeric matrix, or do you read it in as some other sort of object like a datastore?) and the exact line(s) of code you run to create the heatmap using the variable created by the first code segment.
KD
KD le 26 Mar 2025
@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 le 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...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Question posée :

KD
le 26 Mar 2025

Commenté :

dpb
le 26 Mar 2025

Community Treasure Hunt

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

Start Hunting!

Translated by