How to plot already binned data
Mostrar comentarios más antiguos
Hi all. I made this code that bins my data (Hs and Tp are 2 1x49 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
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(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)
4 comentarios
The 'rectangle' method isn't the most optimal. I suggest using imagesc() or histogram2(). Here's another example that assigns labels to each 2D bin.
How do the two 1x49 matrices define the 2D bin counts?
Christian Scalia
el 28 de En. de 2021
Adam Danz
el 28 de En. de 2021
I'm trying to understand the goal conceptually.
From what I understand you have two vectors that are 1x49 and you want to display the data in a 2D grid. How do those two vectors define each value of the grid? For example, let's say the 2D grid (matrix) is called M, how do I map the value of M(i,j) to the two vectors?
Christian Scalia
el 28 de En. de 2021
Editada: Christian Scalia
el 28 de En. de 2021
Respuesta aceptada
Más respuestas (1)
As written, you don't have counts so I would use histogram2 or histcounts2.
% Create sample Hs and Tp matrices
Hs = 15*rand(1, 49);
Tp = 15*rand(1, 49);
datay = Hs;
datax= Tp;
edgesx = 0:0.5:15;
edgesy= 0:0.5:15;
N = histcounts2(datax, datay, edgesx, edgesy);
h = histogram2(datax, datay, edgesx, edgesy);
If you do have the counts in each cell, you could create a heatmap. In this case, if you're not using histogram2 I'm assuming you created N from the example above in some other way.
heatmap(edgesx(1:end-1), edgesy(1:end-1), N)
2 comentarios
Christian Scalia
el 28 de En. de 2021
Editada: Christian Scalia
el 28 de En. de 2021
For missing data, store NaN values in the array. There are a few properties associated with how missing data is displayed. The standardizeMissing function may be of use to you in converting the 0's into NaN's.
A = [1 2 3; 4 5 NaN; 7 8 9];
heatmap(A)
figure
heatmap(A, 'MissingDataColor', 'g', 'MissingDataLabel', 'Nothing to see here')
To control the order in which the Y data is displayed, change the YDisplayData property.
h = heatmap(A, 'MissingDataColor', 'w');
h.YDisplayData = flip(h.YDisplayData);
Categorías
Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






