fill area between two polar curves
66 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
fima v
am 17 Feb. 2017
Kommentiert: Star Strider
am 5 Jun. 2024
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160820/image.jpeg)
phi=0.87*sin((log(r)*pi)/(log(tau)));
phi_shifted_45=0.87*sin((log(r)*pi)/(log(tau)))+0.78;
0 Kommentare
Akzeptierte Antwort
Star Strider
am 17 Feb. 2017
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 Kommentare
Darcy Cordell
am 5 Jun. 2024
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
am 5 Jun. 2024
@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.)
Weitere Antworten (2)
Nate Roberts
am 28 Okt. 2021
Bearbeitet: Nate Roberts
am 28 Okt. 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
5 Kommentare
Mohammed Aldosri
am 22 Nov. 2021
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
Ludovico Saint Amour di Chanaz
am 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')
0 Kommentare
Siehe auch
Kategorien
Mehr zu Polar Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!