How to Create a Equidistant histogram

5 views (last 30 days)
peng peng
peng peng on 8 Oct 2023
Edited: dpb on 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 Comments
Torsten
Torsten on 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 on 8 Oct 2023
Sorry. It's my fault. Thanks for you patience :D

Sign in to comment.

Accepted Answer

dpb
dpb on 8 Oct 2023
Edited: dpb on 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 Comments
dpb
dpb on 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 on 9 Oct 2023
Thank you for your reply. I'll continue to choose "imagesc" function.

Sign in to comment.

More Answers (2)

dpb
dpb on 8 Oct 2023
Edited: dpb on 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...

Image Analyst
Image Analyst on 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.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!