2D histogram plot for N x M matrix

31 vues (au cours des 30 derniers jours)
BeeTiaw
BeeTiaw le 6 Fév 2020
Commenté : Sangjae le 13 Août 2020
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''

Réponse acceptée

Adam Danz
Adam Danz le 6 Fév 2020
Modifié(e) : Adam Danz le 6 Fév 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)))
See the histogram2() link to learn how to set the x and y grid in the bivariate histogram.
  1 commentaire
Sangjae
Sangjae le 13 Août 2020
you are beautiful

Connectez-vous pour commenter.

Plus de réponses (1)

BeeTiaw
BeeTiaw le 7 Fév 2020
Thanks! it works!

Community Treasure Hunt

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

Start Hunting!

Translated by