How to make a fill for standard deviation around a polar plot?
Show older comments
Hello,
I am trying to figure out how to make a standard deviation fill around the mean plotted in polarplot(). Here is my code:
deg = [330 300 270 240 210 180 150 120 90 60 30 0 330];
m_1 = [0.895 0.76 1.35 2.07 2.234 1.167 0.367 0.332 0.410 0.842 0.942 0.760 0.895];
s_1 = [1.505 1.127 1.60 2.53 2.18 1.82 0.437 0.41 0.49 0.88 1.20 1.53 1.505];
figure
pax = polaraxes;
polaraxes(pax)
polarplot(deg2rad(deg),m_1,'r-','LineWidth',2)
How can I add a translucent fill around my line to represent the standard deviation?
Thanks!
1 Comment
Stephanie Reeves
on 3 Jun 2022
Did you figure out how to do this? I am also trying to accomplish this!
Answers (1)
The Patch object cannot be directly used as a child of the PolarAxes object. To achieve the desired effect, a transparent Cartesian axis can be overlaid on the polar axes, and the patch can be created on the Cartesian axes. It's important to convert the standard deviation, mean, and theta into Cartesian coordinates. A custom function can be implemented as shown below:
figure
pax = polaraxes;
hold on
% Plot the mean line
polarplot(pax, theta, m_1, 'r-', 'LineWidth', 2)
% Use polarfill to fill the area between upper and lower bounds
fillPolarArea(pax, theta, theta, lower_bound, upper_bound, 'r', 0.2);
% Ensure the plot is displayed correctly
hold off
function fillPolarArea(polarAx, thetaLower, thetaUpper, radiusLower, radiusUpper,...
fillColor, transparency)
% Overlay a new Cartesian axes on the given polar axes
cartesianAx = axes('Position', polarAx.Position, 'Color', 'none');
% Transform polar coordinates to Cartesian for filling
[xLower, yLower] = pol2cart(thetaLower, radiusLower);
[xUpper, yUpper] = pol2cart(fliplr(thetaUpper), fliplr(radiusUpper));
% Draw filled area between the curves using Cartesian coordinates
fill(cartesianAx, [xLower, xUpper], [yLower, yUpper], fillColor, ...
'FaceAlpha', transparency, 'EdgeAlpha', 0);
% Adjust limits of Cartesian axes to match polar plot and hide them
radiusLimit = max(get(polarAx, 'RLim'));
xlim(cartesianAx, [-radiusLimit, radiusLimit]);
ylim(cartesianAx, [-radiusLimit, radiusLimit]);
axis(cartesianAx, 'square', 'off');
end
This function creates a Cartesian axis at the same position as the polar axes, transforms the points into Cartesian form, and plots a filled area using the fill command. The axis limits are then adjusted, and the axis is made invisible. Note that standard deviation values might need to be scaled down; in the example image, they were scaled down to one-tenth.

For further details, please refer to the following documentation links:
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!