2D histogram plot for N x M matrix
31 views (last 30 days)
Show older comments
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''
0 Comments
Accepted Answer
Adam Danz
on 6 Feb 2020
Edited: Adam Danz
on 6 Feb 2020
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)))
More Answers (1)
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!