How to Create a Equidistant histogram
Mostra commenti meno recenti

% The edge of each bin
Xedges = [10,22,36,49,55,67,77];
Yedges = [21,30,55,70,85,93];
% Obtain the sample count in each bin
X1 = rand(1, 10000) * 100;
Y1 = rand(1, 10000) * 100;
X2 = rand(1, 10000) * 100;
Y2 = rand(1, 10000) * 100;
N(:,:,1) = histcounts2(X1, Y1, Xedges, Yedges);
N(:,:,2) = histcounts2(X2, Y2, Xedges, Yedges);
N = squeeze(mean(N,3));
Hi,
I have a matrix N representing the number of samples in each bin, and I know the boundary (Xedges and Yedges) for each bin. How can I create a histogram for matrix N?
I want the box values (in red) to represent the sample count in the corresponding x1-x2 and y1-y2 bin, and the axis scales to correspond to the interval boundaries. Even though the intervals for each bin are unevenly spaced, I would like the resulting graph to have consistent grid sizes.
11 Commenti
If you want to create random integers, why don't you use "randi" instead of "rand" ?
And N is a vector, not a matrix. So I don't know what kind of histogram you want to plot.
Maybe you mean
N = (N1+N2)/2
instead of
N = mean(N1+N2)
?
peng peng
il 8 Ott 2023
Torsten
il 8 Ott 2023
Did you experiment with "histogram2" ?
peng peng
il 8 Ott 2023
Torsten
il 8 Ott 2023
h=histogram2((X1+X2)/2, (Y1+Y2)/2,Xedges,Yedges,'DisplayStyle','tile','ShowEmptyBins','on');
And manipulating the bin width is cheating :-)
peng peng
il 8 Ott 2023
peng peng
il 8 Ott 2023
I cannot change the distances between the edges, but I think the histogram is correct.
And I think you shouldn't change the bin sizes in the graphics because they are the main reason for the colors.
% The edge of each bin
Xedges = [10,22,36,49,55,67,77];
Yedges = [21,30,55,70,85,93];
% Obtain the sample count in each bin
X1 = rand(1, 10000) * 100;
Y1 = rand(1, 10000) * 100;
X2 = rand(1, 10000) * 100;
Y2 = rand(1, 10000) * 100;
N(:,:,1) = histcounts2(X1, Y1, Xedges, Yedges);
N(:,:,2) = histcounts2(X2, Y2, Xedges, Yedges);
N = squeeze(mean(N,3));
X = [];
Y = [];
for i = 1:numel(Xedges)-1
for j = 1:numel(Yedges)-1
x = (Xedges(i)+Xedges(i+1))/2;
y = (Yedges(j)+Yedges(j+1))/2;
X = [X;x*ones(round(N(i,j)),1)];
Y = [Y;y*ones(round(N(i,j)),1)];
end
end
histogram2(X,Y,Xedges,Yedges,'DisplayStyle','tile','ShowEmptyBins','on');
colorbar
peng peng
il 8 Ott 2023
Torsten
il 8 Ott 2023
N1 = histcounts2((X1+X2)/2, (Y1+Y2)/2,Xedges,Yedges);
N(:,:,1) = histcounts2(X1, Y1, Xedges, Yedges);
N(:,:,2) = histcounts2(X2, Y2, Xedges, Yedges);
N2 = squeeze(mean(N,3));
I didn't use this method in my last response, but thanks for your appreciation.
peng peng
il 8 Ott 2023
Risposta accettata
Più risposte (2)
% The edge of each bin
Xedges = [10,22,36,49,55,67,77];
Yedges = [21,30,55,70,85,93];
% Obtain the sample count in each bin
X1 = randi(100, [10000,1]);
X2 = randi(100, [10000,1]);
Y1 = randi(100, [10000,1]);
Y2 = randi(100, [10000,1]);
N(:,:,1)=histcounts2(X1, Y1, Xedges, Yedges);
N(:,:,2)=histcounts2(X2, Y2, Xedges, Yedges);
N = squeeze(mean(N,3));
X=0:numel(Xedges)-1;
Y=0:numel(Yedges)-1;
hHG2=histogram2('XBinEdges',X,'YBinEdges',Y,'BinCounts',N,'DisplayStyle','tile','ShowEmptyBins','on');
will put the mean N counts in uniformly-spaced bins; however, you can't then change the bin edges displayed on the axes without changing the spacing which goes with it; unlike a regular axes for which the x,y tick display values are not inextricably tied to the x,y tick values. So, histogram2() doesn't look like a real good match here...
This doesn't get you all the way home, but I've got other commitments at the moment and I don't have a clear solution in mind at the moment -- heatmap would work to get uniform mesh and counts, but it labels the bin centers, not the edges.
You may have to make the labels invisible on the histogram2 axes and then overlay another axes to label...
figure
% using IA's idea...
imagesc(X,Y,flipud(N.')) % images are from upper LH corner instead...
xl=xlim; yl=ylim;
xticks(linspace(xl(1),xl(2),numel(X)))
xticklabels(Xedges)
yticks(linspace(yl(1),yl(2),numel(Y)))
yticklabels(Yedges)
Colors seem a little washed out in comparison, but gets the specifics requested. I'd forgotten how @doc:imagesc expands the pixels to an input x,y instead of just displaying a single pixel...
1 Commento
peng peng
il 8 Ott 2023
Categorie
Scopri di più su Data Distribution Plots in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




