Plot Matrix as Heatmap with different cell size width

26 visualizaciones (últimos 30 días)
Mike
Mike el 13 de Ag. de 2021
Editada: Adam Danz el 13 de Ag. de 2021
Hi everyone,
I have a matrix like the one below and would like to plot it as a heatmap, but the cell width of every row and colum width should be different.
For example:
x = [ 1 2 3; 4 5 6]
Cell width:
Row 1: 10 mm; Row 2: 20 mm; Row 3: 25 mm
Colum 1: 5 mm; Colum 2: 15 mm; Colum 3: 20 mm
Is this possible with a heatmap? Or is there another better way to plot this?
Greetings
Mike

Respuesta aceptada

Adam Danz
Adam Danz el 13 de Ag. de 2021
Editada: Adam Danz el 13 de Ag. de 2021
Not possible with heatmap. Here's a solution using surf.
% Inputs
data = [ 1 2 3; 4 5 6];
rowWidths = [10, 20]; %mm, one for each row of data; applied bottom to top
colWidths = [5 15 20]; %mm, one for each col of data; applied left to right
% Produce figure
fig = figure();
ax = axes(fig);
% Plot image
[xg,yg] = meshgrid([0,cumsum(colWidths)], [0,cumsum(rowWidths)]);
sh = surf(xg, yg, zeros(size(xg)), padarray(data,[1,1], 0, 'post'));
% Set colormap (this one is same as heatmap)
n = 255;
cmap = [linspace(.9,0,n)', linspace(.9447,.447,n)', linspace(.9741,.741,n)'];
colormap(ax,cmap)
cb = colorbar(ax);
axis tight
axis equal
view(2)
xlabel('mm')
ylabel('mm)')
To add text labels similar to heatmap,
% find rectangle centers
xCnt = xg(1:end-1,1:end-1) + diff(xg(1,:))/2;
yCnt = yg(1:end-1,1:end-1) + diff(yg(:,1))/2;
th = text(xCnt(:), yCnt(:), compose('%g',data(:)), ...
'HorizontalAlignment','center',...
'VerticalAlignment','middle', ...
'FontSize', 10, 'Color', 'w');
If you want the axis size to reflect the axes scales (ie, if you want the axis size to be 40x30mm), you'll need to set the axis size before plotting. It will look something like,
% Compute axis and figure size
axisSize = [sum(rowWidths), sum(colWidths)]/10; %cm, [width,height]
margins = 1; %cm margin size
figSize = (axisSize + margins*[2,2]); % cm
% Produce figure
fig = figure('Units','centimeters');
fig.Position(3:4) = figSize; % note: there is a min fig size
movegui(fig)
ax = axes('Units','centimeters',...
'Position',[margins,margins,axisSize]);

Más respuestas (0)

Categorías

Más información sobre Colormaps en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by