2D colour coded plot with already binned data

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?

 采纳的回答

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 个评论

Sorry, I should have mentioned that my bin sizes are irregular and imagesc keeps converting the intervals into regulary spaced ones.
Could you provide an example? Or better yet, the data?
At the very least, please describe the variables 'x', 'y' and 'c' where x and y are your edges and c is your matrix.
  • What are the length of x and y?
  • Are x and y monotonically increasing?
  • Do they have duplicate values?
  • Does 'c' just described the color value of each rectangle?
  • Do you expect to have a plot with a variety of rectangles of different sizes and colors?
  • Will any of the rectangles overlap?
I've attached the files for c and x and y edges.
x and y are both (1, 33) and are monotonically increasing. There aren't any duplicates in the edge values if that's what you mean. 'c' would correspond to the colour value of each rectangle. None of the rectangles would overlap.
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).
Precisely what I wanted. Thank you very much.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File 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