The function 'polarhistogram' can produce overlapping bins

Good day
I am experiencing an unexpected behavior of the function 'polarhistogram'. Here is my code:
thetaEx = [0.1 1.1 5.4 3.4 2.3 4.5 3.2 3.4 5.6 2.3 2.1 3.5 0.6 6.1];
%see https://www.mathworks.com/help/matlab/ref/polarhistogram.html
figure('Name','Plausible histogram');
polarhist1 = polarhistogram(thetaEx);
Edges1 = polarhist1.BinEdges; %-> (0:2*pi/3:2*pi) : 1x4 double
figure('Name','histogram with intersecting bins');
polarhist2 = polarhistogram(thetaEx+2*pi/3);
Edges2 = polarhist2.BinEdges; %-> (pi/2:3*pi/4:(2*pi+3*pi/4)) : 1x4 double
Overlap2 = Edges2(end)-Edges2(1)-2*pi; %-> pi/4
%These numbers confirm an overlap.
figure('Name','histogram made plausible');
polarhist3 = polarhistogram(mod(thetaEx+2*pi/3,2*pi));
Edges3 = polarhist3.BinEdges; %equals Edges1
Here are the figures.
The bins of the second histogram are clearly overlapping, which is confirmed by the property 'BinEdges'. This is not what one would expect of a histogram.
The third histogram shows that this unexpected behavior varies depending on shifts by multiples of 2*pi, although this transformation does not modify an angular distribution.
Could one indicate whether I am missing a necessary condition for using the function, or another important detail?
Thanks in advance,
Alexandre
The version of Matlab used: R2021b
Edit
The second histogram changes when the same code is run in Matlab R2022b. The new edges contain no overlap:
Edges2 = polarhist2.BinEdges; %R2022b: 2*pi/3:2*pi/3:8*pi/3
The new figure is a correct histogram, looking as follows:

5 Comments

This behavior can be reproduced in Matlab R2021b Update 1 and 2, as well as in Matlab R2022a.
Since the edges are clearly described as having a range or 2pi in the documentation but the edges generated by the 2nd figure have a range of 7.0686, I'd agree that this is unexpected. Unfortunately polarhistogram uses a primative histogram function wrapped in p-code so I couldn't see what's causing the problem.
Wrapping the values to [0,2pi] as you're doing in the 3rd example is a workaround. If you want to apply this to many polarhistograms, you could us a wrapper such as
wrapvals = @(x)mod(x,2*pi);
x = thetaEx+2*pi/3;
polarhistogram(wrapvals(x))
however, be aware of edge cases. 2pi will wrap to 0. If that's a problem for what you need, you can define wrapvals as a local function instead of an anonymous function and check for those edge cases.
Thank you for interest!
The workaround of wrapping the values to [0,2pi] is sensible and helps in this case. Replacing 2pi by 0 is not a problem, since (I hope), the rest of my functions dealing with angular data are robust to shifts by 2pi. It can be wrapped in a function handle, like you presented, or in a local function like this:
function PolarHist = WrapAndPolarHisto(InPax,InData)
WrappedData = mod(InData,2*pi);
PolarHist = polarhistogram(InPax,WrappedData);
end
However, it's hard to be sure this workaround works as expected for any possible input, until either this behavior of 'polarhistogram' is fixed, or a necessary condition of use is presented in the documentation.
I can't imagine a circumstance where wrapped radians to [0,2pi] would cause a problem since the range of radians in a polar plot are the same interval, [0,2pi].
You could contact tech support to report this issue. If they provide a better workaround or explain why this is not a bug, please share their feedback.
Update: This bug was fixed in MATLAB R2022b.

Sign in to comment.

 Accepted Answer

Hello,
It is my understanding that you are facing the issue of overlapping bins in some cases when you are using the “polarhistogram” function.
This issue is brought to the notice of our developers and they may be investigating further.
You may refer to the below example which is a workaround by using "mod" function:
thetaEx = [0.1 1.1 5.4 3.4 2.3 4.5 3.2 3.4 5.6 2.3 2.1 3.5 0.6 6.1];
%see https://www.mathworks.com/help/matlab/ref/polarhistogram.html
figure('Name','Plausible histogram');
polarhist1 = polarhistogram(thetaEx);
Edges1 = polarhist1.BinEdges; %-> (0:2*pi/3:2*pi) : 1x4 double
figure('Name','histogram with intersecting bins');
polarhist2 = polarhistogram(mod(thetaEx+2*pi/3,2*pi));% THIS LINE IS ONLY MODIFIED FROM YOUR CODE
Edges2 = polarhist2.BinEdges; %-> (pi/2:3*pi/4:(2*pi+3*pi/4)) : 1x4 double
Overlap2 = Edges2(end)-Edges2(1)-2*pi; %-> pi/4
%These numbers confirm an overlap.
figure('Name','histogram made plausible');
polarhist3 = polarhistogram(mod(thetaEx+2*pi/3,2*pi));
Edges3 = polarhist3.BinEdges; %equals Edges1

4 Comments

Thanks for bringing the issue to the notice of the developers! I hope, it will be resolved soon.
Update: This bug was fixed in MATLAB R2022b.
Thanks to Mathworks for your job! I am glad to be able to use the official instruction again, without workaround.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!