How to make all of my heatmaps the same size?

11 visualizaciones (últimos 30 días)
KD
KD el 26 de Mzo. de 2025
Comentada: dpb el 26 de Mzo. de 2025
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

Respuesta aceptada

dpb
dpb el 26 de Mzo. de 2025
Editada: dpb el 26 de Mzo. de 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 comentarios
KD
KD el 26 de Mzo. de 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 el 26 de Mzo. de 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...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by