2D histogram plot for N x M matrix

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