fill area between two polar curves
Show older comments
Hello , i have these two formulas, i would like to fill the area between these two curves is there a way to do it with patch command?
Thanks

phi=0.87*sin((log(r)*pi)/(log(tau)));
phi_shifted_45=0.87*sin((log(r)*pi)/(log(tau)))+0.78;
Accepted Answer
More Answers (3)
@Star Strider asked, "Note to MathWorks: Can we have a patch for polar coordinate systems when you have time to implement it?"
⭐Starting in R2025a, polar axes accept patch objects
A patch can be used to fill beween two lines by stitching the endpoints of the lines together like this (credit to OP and Star Strider for the demo data).
tau = 0.5;
r = linspace(eps, 1.5, 500);
phi = 0.87*sin((log(r)*pi)/(log(tau)));
phi2 = 0.87*sin((log(r)*pi)/(log(tau)))+0.78;
figure()
pax = polaraxes();
patch(pax, [phi, flip(phi2)], [r, flip(r)], 'cyan', FaceAlpha = 0.4)
The patch vertices in polar axes are connected using Euclidean lines which is why there is a straight line segment on the outer edge. Converting that to a curved connection takes a bit more work. You have to generate the curved cooridinates and insert them into the vector of patch coordinates.
It's generally a bad idea to interpolate polar data due to the potential of angular data crossing the 0|360 border. That wouldn't be a problem for this example but I'll provide a more general solution that interpolates in Cartesian coordinates.
% [a b; c d] theta end points connect from a->b and c->d
endpoints = [phi(end), phi2(end);
phi2(1), phi(1)];
xEndpoints = cos(endpoints);
yEndpoints = sin(endpoints);
n = 10; % number of points in connection line
x = diff(xEndpoints,[],2)*(0:1/(n-1):1)+xEndpoints(:,1);
y = diff(yEndpoints,[],2)*(0:1/(n-1):1)+yEndpoints(:,1);
thConnector = atan2(y,x);
rConnector = r([end,1])'*ones(1,n);
thTotal = [phi, thConnector(1,:), flip(phi2), thConnector(2,:)];
rTotal = [r, rConnector(1,:), flip(r), rConnector(2,:)];
figure()
pax = polaraxes();
patch(pax, thTotal, rTotal, 'cyan', FaceAlpha = 0.4)
Read more about patches and surfaces in polar axes: https://blogs.mathworks.com/graphics-and-apps/2025/09/30/polar-plots-with-patches-and-surfaces-r2025a/
2 Comments
Star Strider
on 11 Jul 2025
Interesting! I wasn't aware of that upgrade.
I'll keep that in mind.
Noam
on 12 Sep 2025
maybe the unrwap function would help when crossing the 0|360 border
Nate Roberts
on 28 Oct 2021
Edited: Nate Roberts
on 28 Oct 2021
I answered a similar question earilier today (https://www.mathworks.com/matlabcentral/answers/340760-how-to-fill-the-area-between-two-curves-on-a-polar-plot#answer_818143), but this one requires a more general solution than the one given there. The idea is the same, to overlay a transparent cartesian axes over the polar axes. This function (polarfill), however, recognizes that you may want to fill between different angles, not just different radii:
tau = 0.5;
r = linspace(eps, 1.5, 500);
phi = @(r)0.87*sin((log(r)*pi)/(log(tau))); %lambda function
phi_shifted_45 = @(r)0.87*sin((log(r)*pi)/(log(tau)))+0.78; %lambda function
f = figure();
polarplot(phi(r),r,'k','LineWidth',2); hold on; ax_pol = gca;
polarplot(phi_shifted_45(r),r,'r','LineWidth',2)
polarfill(ax_pol,phi(r),phi_shifted_45(r),r,r,'b',0.5)
%% Filling the extra gap
r_upper = 1.5; % The upper radius is constant
theta_range = linspace(phi(r_upper),phi_shifted_45(r_upper)); % The range of theta for the area
% Determining the polar equation for the lower radius as a function of theta
[x1,y1] = pol2cart(theta_range(1),r_upper); % Polar -> Cartesian
[x2,y2] = pol2cart(theta_range(end),r_upper); % Polar -> Cartesian
m = (y2-y1)/(x2-x1); % Slope of a line
b = y1-m*x1; % Intercept of a line
r_lower = - b ./ (m*cos(theta_range)-sin(theta_range)); % polar equation of a straight line
% Second call to Polar Fill
polarfill(ax_pol,theta_range,theta_range,r_lower,r_upper,'b',0.5)
function polarfill(ax_polar,thetal,thetah,rlow,rhigh,color,alpha)
ax_cart = axes();
ax_cart.Position = ax_polar.Position;
[xl,yl] = pol2cart(thetal,rlow);
[xh,yh] = pol2cart(fliplr(thetah),fliplr(rhigh));
fill([xl,xh],[yl,yh],color,'FaceAlpha',alpha,'EdgeAlpha',0);
xlim(ax_cart,[-max(get(ax_polar,'RLim')),max(get(ax_polar,'RLim'))]);
ylim(ax_cart,[-max(get(ax_polar,'RLim')),max(get(ax_polar,'RLim'))]);
axis square; set(ax_cart,'visible','off');
end
6 Comments
Rosa
on 28 Oct 2021
This is very nice, thank you! However, it does not work if I change rlim before calling the polarfill function
Star Strider
on 28 Oct 2021
NOTE — This does not fill all the way to the circumference (between about 310° and 395°). That required a separate step in my code.
Nate Roberts
on 28 Oct 2021
@Rosa, that's correct. We ran into the same issue last night generating a figure for a publication. The solution is to shift everything so all the data is positive and the lower limit on RLim is 0. Then use rticks() and rticklabels() to hide the shift! Hope that helps!
Nate Roberts
on 28 Oct 2021
@Star Strider I've updated the answer to fill the extra gap.
how would this function be implemented in appdesigner? I tried to use polarfill, but when I run the app, a new figure pops for me with random stuff in it instead of coloring the polaraxes I created in the UIFigure. This is the code i run in the appdesigner
Pax = polaraxes(app.UIFigure);
Pax.Units = 'pixels';
Pax.Position = [478 14 430 297];
theta1 = (22.5*pi/180):0.01*pi:(67.5*pi/180);
rho1 = 2*ones(size(theta1));
polarplot(theta1, rho1);
Rlow = 0;
Rhigh = 2;
ax_cart = axes();
ax_cart.Position = Pax.Position;
[XL, YL] = pol2cart(theta1, Rlow);
[XH, YH] = pol2cart(fliplr(theta1), fliplr(Rhigh));
fill([XL,XH],[YL,YH],'blue','FaceAlpha',0.4,'EdgeAlpha',0);
xlim(ax_cart,[-max(get(Pax,'RLim')),max(get(Pax,'RLim'))]);
ylim(ax_cart,[-max(get(Pax,'RLim')),max(get(Pax,'RLim'))]);
axis square; set(ax_cart,'visible','off');
Pax.ThetaZeroLocation = 'top';
Pax.ThetaLim = [-180 180];
Pax.RLim = [0 1];
Pax.ThetaTick = -180:45:180;
I run similar code in a .m file and I get this
Pax = polaraxes;
theta = (22.5*pi/180):0.01*pi:(67.5*pi/180);
rho = 2*ones(size(theta));
polarplot(theta, rho);
rlim([0 1]);
polarfill(Pax, theta, 0, 1,'green', 0.3);
function polarfill(ax_polar,theta,rlow,rhigh,color,alpha)
ax_cart = axes();
ax_polar.ThetaLim = [-180 180];
ax_polar.ThetaZeroLocation = 'top';
ax_polar.ThetaTick = -180:45:180;
ax_cart.Position = ax_polar.Position;
[xl,yl] = pol2cart(theta,rlow);
[xh,yh] = pol2cart(fliplr(theta),fliplr(rhigh));
fill([xl,xh],[yl,yh],color,'FaceAlpha',alpha,'EdgeAlpha',0);
xlim(ax_cart,[-max(get(ax_polar,'RLim')),max(get(ax_polar,'RLim'))]);
ylim(ax_cart,[-max(get(ax_polar,'RLim')),max(get(ax_polar,'RLim'))]);
axis square; set(ax_cart,'visible','off');
end
could you help me?
Pax = polaraxes;
theta = (22.5*pi/180):0.01*pi:(67.5*pi/180);
rho = 2*ones(size(theta));
polarplot(theta, rho);
rlim([0 1]);
polarfill(Pax, theta, 0, 1,'green', 0.3);
function polarfill(ax_polar,theta,rlow,rhigh,color,alpha)
ax_cart = axes();
ax_polar.ThetaLim = [-180 180];
ax_polar.ThetaZeroLocation = 'top';
ax_polar.ThetaTick = -180:45:180;
ax_cart.Position = ax_polar.Position;
[xl,yl] = pol2cart(theta,rlow);
[xh,yh] = pol2cart(fliplr(theta),fliplr(rhigh));
fill([xl,xh],[yl,yh],color,'FaceAlpha',alpha,'EdgeAlpha',0);
xlim(ax_cart,[-max(get(ax_polar,'RLim')),max(get(ax_polar,'RLim'))]);
ylim(ax_cart,[-max(get(ax_polar,'RLim')),max(get(ax_polar,'RLim'))]);
axis square; set(ax_cart,'visible','off');
end
Noam
on 12 Sep 2025
I had some issues with this, but I changed added the unwrap function to the angle data, and that fixed everything (no fliplr needed).
[xl,yl] = pol2cart(unwrap(theta),rlow);
[xh,yh] = pol2cart(unwrap(theta),rhigh)
Ludovico Saint Amour di Chanaz
on 2 Jun 2022
I you need to do a subplot with this fill the polarfill axes are not aligned and it makes for a very messy fill, this can be solved easily with the 'align' function of subplot
subplot(rows, cols, i, 'align')
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!




