MATLAB Answers


Plotting hitmap with different box size

Asked by Mudasser Seraj on 27 Jul 2018
Latest activity Commented on by Mudasser Seraj on 30 Jul 2018
I have a 64x9 matrix and I want to plot a heatmap with different box sizes. Figure as below.
Can someone please help me with this? Added sample data file


Neither heatmap() or HeatMap() (from Bioinformatics toolbox) can do this for you.
You might want to look in the File Exchange to see if you can find some code to adapt.
Yeah. That's why I am asking help if someone can do this with codes. I couldn't find anything like this in File Exchange.
on 27 Jul 2018
You can do this quite easily with scatter3. I'll give it a try.

Sign in to comment.

2 Answers

Answer by Kelly Kearney
on 27 Jul 2018
 Accepted Answer

You can create this pretty easily using patches:
The test data:
c = rand(20,9)*2 - 1; % Color value
s = c; % Scale value
% Coordinates for a box
xbox = [-1 -1 1 1 -1]*0.5;
ybox = [-1 1 1 -1 -1]*0.5;
% Coordinates for the box centers
[yc,xc] = ndgrid(1:size(c,1), 1:size(c,2));
xc = xc + 0.5;
yc = yc + 0.5;
% Scale the box and add to center coordinates
x = xc(:) + xbox.*s(:);
y = yc(:) + ybox.*s(:);
% Plot
patch(x',y',c(:),'edgecolor', 'none');
axis tight equal;
set(gca, 'clim', [-1 1]);
Note that in this example I'm using the implicit expansion that was introduced recently (R2017 or so)... in older versions you'll have to use repmat, bsxfun, etc.

  1 Comment

Thank you so much.

Sign in to comment.

Answer by jonas
on 27 Jul 2018
Edited by jonas
on 27 Jul 2018

Here's an alternative using scatter3.
%%Some data
figure;hold on
%%Scale for color
cn = ceil(max(zc(:)));
cm = colormap(parula(cn));
%%Scale Z for box size
%%plot and fix visuals
h=scatter3(X(:), Y(:), Z(:), zb,cm(ceil(zc),:),'s','filled')
caxis([-1 1])
box on
grid on
See attachment

  1 Comment

Thanks a lot.

Sign in to comment.