MATLAB Answers

Plot Matrix as Heatmap with different cell size width

조회 수: 6(최근 30일)
Mike 2021년 8월 13일
편집: Adam Danz 2021년 8월 13일
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?

채택된 답변

Adam Danz
Adam Danz 2021년 8월 13일
편집: Adam Danz 2021년 8월 13일
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)'];
cb = colorbar(ax);
axis tight
axis equal
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(:)), ...
'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
ax = axes('Units','centimeters',...

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by