2D colour coded plot with already binned data

4 次查看(过去 30 天)
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?

采纳的回答

Adam Danz
Adam Danz 2019-2-11
编辑:Adam Danz 2021-1-7
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
imagesc(x,y,c)
grid on
colorbar
Using heatmap()
See link for additional input options.
% ...continuing from above
heatmap(x,y,c)
colormap('parula')
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
figure
axh = axes;
hold(axh, 'on')
for i = 1:numel(cIdx)
% Don't draw rectangle if color value is 0
if cIdx(i) == 0
continue
end
% Draw rectangle
rh = rectangle(axh, 'Position', [x(i), y(i), w(i), h(i)], ...
'FaceColor', cmat(cIdx(i), :), 'EdgeColor', 'k');
end
% Plot cosmetics
grid(axh, 'on')
colormap(axh, 'parula')
colorbar(axh)
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
xlim(xl)
ylim(yl)
  5 个评论
Adam Danz
Adam Danz 2019-2-11
编辑:Adam Danz 2019-2-11
I updated my solution. See the 3rd section for code that works with your data and produces the following plot (excluding the sanity check at the end of the code).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by