Effacer les filtres
Effacer les filtres

How to Create a Equidistant histogram

2 vues (au cours des 30 derniers jours)
peng peng
peng peng le 8 Oct 2023
Modifié(e) : dpb le 9 Oct 2023
% 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 commentaires
Torsten
Torsten le 8 Oct 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
peng peng le 8 Oct 2023
Sorry. It's my fault. Thanks for you patience :D

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 8 Oct 2023
Modifié(e) : dpb le 8 Oct 2023
Well, the prior has some things of academic interest so I won't delete it, but actually when got a moment to poke around, it turns out the histogram is a Child of an ordinary axes so you can do what you want with the ticks and tick labels after all...just not on the histogram itself but its parent. So, everything above is ok, just a couple more steps to finish from where left off..
% 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');
hAx=hHG2.Parent; % get the parent axes holding the histogram
xticks(hAx,X), xticklabels(hAx,Xedges) % per Bruno's admonition, make sure working on intended axes
yticks(hAx,Y), yticklabels(hAx, Yedges)
  3 commentaires
dpb
dpb le 8 Oct 2023
It isn't possible for a histogram to have negative counts and, unfortunately for this purpose, Mathworks enforces that the bin counts have to be nonnegative. If, indeed, N would have negative values in your application then histogram is totally out as a solution -- as, of course, would be @doc:histcounts2 as well.
peng peng
peng peng le 9 Oct 2023
Thank you for your reply. I'll continue to choose "imagesc" function.

Connectez-vous pour commenter.

Plus de réponses (2)

dpb
dpb le 8 Oct 2023
Modifié(e) : dpb le 9 Oct 2023
% 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...
ADDENDUM: Using @Image Analyst's idea...
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 commentaire
peng peng
peng peng le 8 Oct 2023
Thanks! it helps me a lot!

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 8 Oct 2023
Instead of using histogram which will plot the edges like you have, if you want equal spacing upon display, regardless of what the bin width is, just use imshow or imagesc to display the 2-D matrix.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by