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

150 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2017-5-18
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 个评论
Star Strider
Star Strider 2017-5-24
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 2017-5-25
If my Answer helped you solve your problem, please Accept it!

请先登录,再进行评论。

更多回答(2 个)

Nate Roberts
Nate Roberts 2021-10-27
编辑:Nate Roberts 2021-10-28
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 个评论
Arthur Vieira
Arthur Vieira 2022-6-20
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-5-18
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 个评论
Walter Roberson
Walter Roberson 2017-5-18
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 2017-5-18
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by