Hi all. I made this code that bins my data (Hs and Tp are 2 1×49 matrices). The data as you can see is already binned in bins that are 0.5 "wide". The bins go from 0 to 15.
datay = Hs;datax= Tp;edgesx = 0:0.5:15;edgesy= 0:0.5:15;valuesx= edgesx(2:end);valuesy= edgesy(2:end);Y= discretize (datay,edgesx,valuesx)X= discretize(datax,edgesy,valuesy)XY=[Y;X]
-valuesx and values y are used to assign bin values.
-Discretize is used to distribute the Hs and Tp numerical values into the X and Y bins
I would like to make a code that plots Y vs X as square (since the data has equal edges of 0.5).
The number inside the bin is the bin count (i.e. how many entries in that bin)
My final result should look like something on the lines of this (ignore those lines)
I attempted to reach this result by changing this code : https://uk.mathworks.com/matlabcentral/answers/444262-2d-colour-coded-plot-with-already-binned-data (author @Adam Danz ) but it doesn't work as I think I'm not carefully thinking about the dimension of the matrix I want to plot
datay = Hs;datax= Tp;edgesx = 0:0.5:15;edgesy= 0:0.5:15;valuesx= edgesx(2:end);valuesy= edgesy(2:end);Y= discretize (datay,edgesx,valuesx)X= discretize(datax,edgesy,valuesy)XY=[Y;X]%-------------Modified Code------------
xedges = 0:0.5:30; %Axis should go from 0-30
yedges= 0:0.5:30; %Axis should go from 0-30% 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)); %Happy with this as the data is already binned in 0.5 wide rectangle
% Normalize c matrix (0:1)
cNorm = (XY - min(XY(:))) / max(XY(:)); % This is what is not working in my case I think
% 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
figureaxh = 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(XY(:)), max(XY(:))])%% 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)
Best Answer