2
votes

Bear with me for a minute as I explain what I am doing. I have 24 angles, between 0 and 360 degrees. At each angle, I have a value. I would like to plot this as a line on a polar plot. Thus, my end result should be a polar plot with 24 bars on it, each pointing in the direction of the angle in radians that is corresponds to. Can this be done in MATLAB or some other plotting utility?

2
Is a rose plot what you're looking for?Suever

2 Answers

1
votes

I think you are after polarplot:

ang_deg = sort(rand(1,24)*360); % some random angles
ang_rad = ang_deg*pi/180; % convert degrees to radians for polarplot
values = rand(1,24); % random values to plot
polarplot(ang_rad, values);

Alas, this function was introduced at version 2016a.
For older versions, use polar instead (thanks Thales for pointing this out).

1
votes

You can use compass for that:

ang = deg2rad(linspace(0,360,24));% angles
vals = 1:24; % values
% convert the values to vector components
U = vals.*cos(ang);
V = vals.*sin(ang);
% plot:
hp = compass(U,V);

and you get:

compass1

However, if you want bars and not arrows it's a little more tricky. After you plot hp from above you should do the following:

% get all X and Y data from the plot:
arrowsX = cell2mat(get(hp,'XData'));
arrowsY = cell2mat(get(hp,'YData'));
% delete all arrows head values:
set(hp,{'XData'},num2cell(arrowsX(:,1:2),2));
set(hp,{'YData'},num2cell(arrowsY(:,1:2),2));
% make the lines look like bars:
set(hp,{'LineWidth'},num2cell(ones(24,1)*6));

compass2

If you have Matlab R2016b you can use polarhistogram:

ang = deg2rad(linspace(0,360,25));% angles
vals = 1:24; % values
polarhistogram('BinEdges',ang,'BinCounts',vals)

but here specifying the BinEdges so that the bins will be cntered to ang is a less direct, and need some manipulation:

ang = rand(24,1)*2*pi; % angles
vals = rand(24,1); % values
% assuming your data is like 'ang' and 'vals' above:
data = sortrows([ang vals],1); % sort the data
% set the width of the bars by the smallest one:
w = min(diff(sort(ang,'ascend')))*0.5; 
% define the bins location:
low = max(w,data(:,1)-w);
high = min(2*pi,data(:,1)+w);
binEdge = [low high].';
% set zeros to all the 'spare' bins:
counts = [data(:,2) zeros(size(data,1),1)].';
counts = counts(:);
% plot:
polarhistogram('BinEdges',binEdge(:),'BinCounts',counts(1:end-1))

And the result (for some random data):

compass3