How to Create a Equidistant histogram

% 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

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)
?
sorry, I post the wrong version, now I have corrected this.
Did you experiment with "histogram2" ?
Yes, I tried it.
However, I only know how to plot every N, but I don't know how to plot averaged N.
h1=histogram2(X1, Y1,Xedges,Yedges,'DisplayStyle','tile','ShowEmptyBins','on');
h1=histogram2(X2, Y2,Xedges,Yedges,'DisplayStyle','tile','ShowEmptyBins','on');
and I don't know hwo to make every bin have the same width and height, thoug the range of every bin is not same
h=histogram2((X1+X2)/2, (Y1+Y2)/2,Xedges,Yedges,'DisplayStyle','tile','ShowEmptyBins','on');
And manipulating the bin width is cheating :-)
I only want the axis scale to represent the edges of the bins, which means the values of Xedges and Yedges. The distance from 10 to 22 on the graph is the same as the distance from 22 to 36.
No, thanks for your answer, but I think (X1+X2)/2 is not the result that I want.
I just care the amout in every bin (type), and I don't care the range of every bin. XD
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
You can compare the value of N1 and N2
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));
The value of N1 and N2 is not same.
Another answer form dpb presents a simple methond.
The size of bins in the graph is unrelated to the values of bins; changing the bin size does not alter the actual values in the bins.
Anywa, thanks for you reply.
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.
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

Thanks! It's really helpful for me.
I have a new question: Can I plot this image if the matrix N contains negative values? Currently, I am using the "imagesc" function to visualize the matrix N, because it contains negative values. Looking forward to your reply
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.
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...
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.

Categories

Products

Release

R2022b

Asked:

on 8 Oct 2023

Edited:

dpb
on 9 Oct 2023

Community Treasure Hunt

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

Start Hunting!