2D colour coded plot with already binned data

17 views (last 30 days)
Ace_of_Shovels on 11 Feb 2019
Edited: Adam Danz on 7 Jan 2021
I have a [32x32] matrix of data that's already binned that I want to make a 2 dimensional color coded plot of - something like this : AdjustHistogram2PropertiesExample_04.png
I also want to be able to define the bin edges/centres. I tried using imagesc but couldn't find a way to specify the bin edges. Is there a way I could maybe add the data to a hhistogram2 object?

Accepted Answer

Adam Danz
Adam Danz on 11 Feb 2019
Edited: Adam Danz on 7 Jan 2021
Here's an example of how to specify bins/edges using fake, random data.
Using imagesc()
See link for additional input options.
% Create fake data
c = rand(32,32);
% Define x and y edges that span from -4 to 4
x = linspace(-4, 4, size(c,2));
y = linspace(-4, 4, size(c,1));
% Produce plot
grid on
Using heatmap()
See link for additional input options.
% ...continuing from above
Using rectangle(), when grid is variable in size (build your own color grid)
If the x and y coordinates of each grid is nonuniform and each grid potentially has a different size, you'll have to build the grid from scratch (if there's a different method I'd love to know).
Using the data shared below in one of the comments left by OP, this example builds the color grid from scratch using rectangle().
% c is a [32x32] matrix
% xedges is a monotonically increasing vector of length 33;
% yedges (same)
c = rand(32,32);
xedges = linspace(-4, 4, size(c,2)+1);
yedges = linspace(-4, 4, size(c,1)+1);
% Define bottom, left corner (x,y) of each rectangle
[x, y] = meshgrid(xedges(1:end-1),yedges(1:end-1));
% Determine width and height of each rectangle
[w, h] = meshgrid(diff(xedges), diff(yedges));
% Normalize c matrix (0:1)
cNorm = (c - min(c(:))) / max(c(:));
% Create color matrix
% * you can use any color map: https://www.mathworks.com/help/matlab/ref/colormap.html#buc3wsn-1-map
% * if you change the color map here, change it below as well.
% * I'm setting precision here to 4 decimal places
prec = 1e4;
cmat = parula(prec);
% Assign color (row index) to each value in c
cIdx = round(cNorm * prec);
% loop through each rectangle and draw it
axh = axes;
hold(axh, 'on')
for i = 1:numel(cIdx)
% Don't draw rectangle if color value is 0
if cIdx(i) == 0
% Draw rectangle
rh = rectangle(axh, 'Position', [x(i), y(i), w(i), h(i)], ...
'FaceColor', cmat(cIdx(i), :), 'EdgeColor', 'k');
% Plot cosmetics
grid(axh, 'on')
colormap(axh, 'parula')
caxis([min(c(:)), max(c(:))])
%% Sanity check
% Confirm that edges are correct by drawing lines at edges.
% Save and restore axis limits
yl = ylim;
xl = xlim;
plot([xedges;xedges], repmat(ylim', size(xedges)), 'k-') %x bins, vertical lines
plot(repmat(xlim', size(yedges)), [yedges;yedges], 'k-') %y bins, horizontal lines
Ace_of_Shovels on 12 Feb 2019
Precisely what I wanted. Thank you very much.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!