2D histogram plot for N x M matrix

조회 수: 50 (최근 30일)
BeeTiaw
BeeTiaw 2020년 2월 6일
댓글: Sangjae 2020년 8월 13일
Hi, I wanted to do make a 2D histogram, something like below
My raw dataset plot is shown below; which I created using the following command
plot(X,Level); xlabel('X');ylabel('Level')
The plot basically plotting a [31 x 390] matrix called 'Level' against vector 'X'. (both data are attached in two separate txt files).
How do I generate a 2D plot using this dataset? I wanted to have a 2D plot like in the first figure
Or anything else that can show 'the number of data points for every 'Level' vs X and plotted against X and Level''

채택된 답변

Adam Danz
Adam Danz 2020년 2월 6일
편집: Adam Danz 2020년 2월 6일
histogram2() creates a bivariate histogram plot that you can apply to your data.
Here's a demo that applies this plot to noisy gaussian curves. Pay attention the variables x and y which will be inputs to histogram2.
Produce the noisy data. You'll already have the data; you just need to make sure it has the same general shape as these x and y variables.
% Produce noise gaussian data
% x is a 1xm or mx1 vector that defines the x values for each curve.
% y is a mxn matrix of m y-values for n curves.
gausFcn = @(X,C,A,S)exp(-(X - C).^2/(2*S^2)) * A; % guassian function (x, center, amp, sigma)
x = 0 : 0.5 : 3000; % The x values for all curves
nCurves = 20; % Number of curves to produce
y = zeros(numel(x),nCurves); % We'll store the y data here
% Produce n noisy curves
for i = 1:nCurves
y(:,i) = gausFcn(x,randi(100)+1200,(rand(1)+1)*4+10,randi(120)+500) + sin(linspace(0,randi(20)*pi,numel(x)));
end
% Show the noisy data
figure()
sph(1) = subplot(2,1,1);
plot(x,y)
grid on
Apply the bivariate histogram
% First, replicate the x values so there's 1 x value for each y value.
xRep = repmat(x, 1, nCurves); % for row vectors; if col vec: repmat(x, nCurves, 1)
% apply the bivariate historgram
sph(2) = subplot(2,1,2);
linkaxes(sph)
h = histogram2(xRep(:),y(:),'DisplayStyle','tile','ShowEmptyBins','on');
Result
Note, if you'd like it to look like the image you shared, set colormap to gray but reverse the order of colors.
colormap(flipud(gray(255)))
See the histogram2() link to learn how to set the x and y grid in the bivariate histogram.

추가 답변 (1개)

BeeTiaw
BeeTiaw 2020년 2월 7일
Thanks! it works!

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by