1

Question

When using polarhistogram(theta) to plot a dataset containing azimuths from 0-360 degrees. Is it possible to specify colours for given segments?

Example

In the plot bellow for example would it be possible to specify that all bars between 0 and 90 degrees (and thus 180-270 degrees also) are red? whilst the rest remains blue?

enter image description here

Reference material

I think if it exists it will be within here somewhere but I am unable to figure out which part exactly:

https://www.mathworks.com/help/matlab/ref/polaraxes-properties.html

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Alexander Peace
  • 113
  • 2
  • 14

1 Answers1

1

If you use rose, you can extract the edges of the histogram and plot each bar one by one. It's a bit of a hack but it works, looks pretty and does not require Matlab 2016b.

theta = atan2(rand(1e3,1)-0.5,2*(rand(1e3,1)-0.5));
n = 25;
colours = hsv(n);

figure;
rose(theta,n); cla; % Use this to initialise polar axes
[theta,rho] = rose(theta,n); % Get the histogram edges
theta(end+1) = theta(1); % Wrap around for easy interation
rho(end+1) = rho(1); 
hold on;
for j = 1:floor(length(theta)/4)
    k = @(j) 4*(j-1)+1; % Change of iterator
    h = polar(theta(k(j):k(j)+3),rho(k(j):k(j)+3));
    set(h,'color',colours(j,:)); % Set the color
    [x,y] = pol2cart(theta(k(j):k(j)+3),rho(k(j):k(j)+3));
    h = patch(x,y,'');
    set(h,'FaceColor',colours(j,:),'FaceAlpha',0.2);
    uistack(h,'down');
end
grid on; axis equal;
title('Coloured polar histogram')

Result

enter image description here

MarcinKonowalczyk
  • 2,577
  • 4
  • 20
  • 26
  • Ok great thanks. This is a starting point but as MATLAB recommends not using Rose maybe this isn't the ideal solution? Is it also possible to 'hack' Polarhistogram? The main issue I seem to come across with 'rose' is that segments that contain the same count of data end up different lengths. – Alexander Peace Mar 29 '17 at 16:37
  • 1
    @AlexanderPeace I'm sorry but I can't help you there. I have Matlab 2015b and so no access to `polarhistogram`. I do think something analogous to my `rose` solution could be done with `[N,edges] = histcounts` to get the histogram and `cart2pol` to plot it in polar coordinates. It's still a heavy workaround though. – MarcinKonowalczyk Mar 29 '17 at 16:45
  • 1
    As far as I looked at the documentation of `polarhistogram`, all the bars are plotted in one go and so you're going to have quite a difficult time getting only one of them to be different color. Almost certainly you'll need to plot a single bar of different color separately. – MarcinKonowalczyk Mar 29 '17 at 16:47