How to fill the area between two curves on a polar plot?

159 views (last 30 days)
My code looks is below. I attached the two curves it generates, with the data that I have. How can I fill the space between the two curves?
t=data(1,:);
a1=data(11,:);
b1=data(12,:);
r_scale=50;
line_width=2;
font_size=12;
marker = 3;
figure(1)
polarplot(t,a1,'-or','MarkerSize',2)
hold on
polarplot(t,b1,'-ok','MarkerSize',2)
hold on

Accepted Answer

Star Strider
Star Strider on 18 May 2017
Yes!
Yours are different. Yours are also an easier problem.
I set up everything in polar coordinates in that code, then used the pol2cart function to create Cartesian representations for them, and plotted them in Cartesian space. My code drew the polar coordinates the same way. It did not use polar or polarplot, since they do not offer the necessary options.
Set your data up in polar coordinates, use pol2cart, patch, then plot.
Use the Plot Full Circumference and Plot Radials section in my code your referred to, to plot the polar coordinate grid. Use the text function for the radial and angle labels if you want them. Use the values in the grid plotting part of my earlier code to get the (x,y) values for your text calls.
This code snippet should get you started:
theta = linspace(0, 2*pi, 18); % Create Data (Angles)
Data1 = rand(1, 18)*0.5 + 0.5; % Create Data (First Radius)
Data2 = rand(1, 18)*0.5; % Create Data (Second Radius)
[x1, y1] = pol2cart(theta, Data1); % Convert To Cartesian
[x2, y2] = pol2cart(theta, Data2);
figure(1)
patch([x1 fliplr(x2)], [y1 fliplr(y2)], 'g', 'EdgeColor','g') % Fill Area Between Radius Limits
hold on
plot(x1, y1, '-k')
plot(x2, y2, '-r')
hold off
axis equal
Experiment to get the result you want. Post back if you have problems. I’ll do my best to help.
  12 Comments
Star Strider
Star Strider on 24 May 2017
The polar and polarplot functions have a limited number of options, so I don’t use them often.
I don’t understand what you want to do. However if I guess correctly, to use LaTeX in my code for the radial and angular labels, use the regular plot commands (such as the text call I used in my code) and set 'Interpreter','latex'. The text function allows that option. See the documentation on text for details.
Star Strider
Star Strider on 25 May 2017
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (2)

Nate Roberts
Nate Roberts on 27 Oct 2021
Edited: Nate Roberts on 28 Oct 2021
I wrote a function that overlays a transparent cartesian axis over the polar axis. This may be cheating a little bit, but it gets the job done and looks nice:
theta = linspace(0,2*pi,180);
rho = 10*ones(size(theta));
f = figure('Color','White');
p = polarplot(theta,rho); rlim([0,15]);
polarfill(gca,theta,rho-normrnd(2,0.2,size(rho)),rho+normrnd(2,0.2,size(rho)),'blue',0.6)
function polarfill(ax_polar,theta,rlow,rhigh,color,alpha)
ax_cart = axes();
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
See also my answer to a similar question. It is a more general solution than the one posted here: https://www.mathworks.com/matlabcentral/answers/325599-fill-area-between-two-polar-curves#answer_818408
  1 Comment
Arthur Vieira
Arthur Vieira on 20 Jun 2022
This solution seems nice but causes me issues. 1. I can't for instance save the figure as doing so will save the figure with just the fill in a cartesian axis. Can only take pictures with PrintScreen. 2. I can't 'hold on' after the fill has been added to plot more stuff. I'd actually like to plot two lines and two fills in the same figure.

Sign in to comment.


Walter Roberson
Walter Roberson on 18 May 2017
Unfortunately that does not appear to be possible. surface() and patch() specifically reject being children of PolarAxes; and fill() and area() and mesh() [none of which are primitives] fail when calling newplot() with newplot() rejecting making a cartesian child of a polar axes.
The actual drawing of polarplot() is by calling plot(), the implementation of which is now private.
  3 Comments
Walter Roberson
Walter Roberson on 18 May 2017
That code works by not using a polarplot() with its polaraxes(): it expects the user to use polar() which uses cartesian axes underneath it, and then it use patch() with cartesian coordinates.
Benjamin Cowen
Benjamin Cowen on 18 May 2017
Could something similar be done for mine? Or is their problem too different from mine? There's appears to be equations for curves, whereas mine is data points, so I'm not sure if the same thing can be applied. If it can, I'm not sure how.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!