fill area between two polar curves

조회 수: 45 (최근 30일)
fima v
fima v 2017년 2월 17일
편집: Adam Danz 2025년 9월 30일
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;

채택된 답변

Star Strider
Star Strider 2017년 2월 17일
The patch function will not work with polar axes. It throws this error:
Error using patch
While setting property 'Parent' of class 'Patch':
Patch cannot be a child of PolarAxes.
Oh, well...
The best I can do with this is to use pol2cart:
tau = 0.5;
r = linspace(eps, 1.5, 500);
phi = 0.87*sin((log(r)*pi)/(log(tau)));
phi_shifted_45 = 0.87*sin((log(r)*pi)/(log(tau)))+0.78;
[x1,y1] = pol2cart(phi, r);
[x2,y2] = pol2cart(phi_shifted_45, r);
figure(1)
patch([x1 fliplr(x2)], [y1 fliplr(y2)], 'g')
axis equal
Note to MathWorks: Can we have a patch for polar coordinate systems when you have time to implement it?
  댓글 수: 4
Darcy Cordell
Darcy Cordell 2024년 6월 5일
Not sure if this is really a solution, but I find it weird that the old function "polar" that was in much earlier releases of MATLAB can handle patches fine. So, if you want to plot a patch, you can just use "polar" instead of "polarplot".
In the later releases, "polar" is not recommend for use and MATLAB suggests users use polarplot instead. Frustrating when updates actually cause a loss in functionality rather than a gain...
Star Strider
Star Strider 2024년 6월 5일
@Darcy Cordell — Note that my code uses strictly Cartesian coordinates (the pol2cart calls), and draws the polar axes grid separately. I have not attempted to do somoething similar with either polar or polarplot. (That may not even have been an option in R2017a.)

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Adam Danz
Adam Danz 2025년 7월 11일
편집: Adam Danz 2025년 9월 30일
@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)
  댓글 수: 2
Star Strider
Star Strider 2025년 7월 11일
Interesting! I wasn't aware of that upgrade.
I'll keep that in mind.
Noam
Noam 2025년 9월 12일
maybe the unrwap function would help when crossing the 0|360 border

댓글을 달려면 로그인하십시오.


Nate Roberts
Nate Roberts 2021년 10월 28일
편집: Nate Roberts 2021년 10월 28일
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
Mohammed Aldosri
Mohammed Aldosri 2021년 11월 22일
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
Noam 2025년 9월 12일
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
Ludovico Saint Amour di Chanaz 2022년 6월 2일
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')

카테고리

Help CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by