필터 지우기
필터 지우기

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

조회 수: 103 (최근 30일)
Benjamin Cowen
Benjamin Cowen 2017년 5월 17일
댓글: Arthur Vieira 2022년 6월 20일
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