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

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

 Réponse acceptée

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 commentaires

First, change the calls to the circumference (and radial) grid lines to match your data:
ccf = @(rd,th) [rd*cos(th); rd*sin(th)]; % Function
cc50 = ccf(50,cfull); % R = 50
cc30 = ccf(30,cfull); % R = 30
cc10 = ccf(10,cfull); % R = 10
If you change the names of the circumferential variables, you need to change them in the plot call as well.
Change the value of rlim to 50 to do that for the radials:
rlim = 50;
rrv = [0:30:360]*pi/180; % Calculate Radials
[rd00x, rd00y] = pol2cart(rrv, zeros(size(rrv)));
[rd15x, rd15y] = pol2cart(rrv, rlim*ones(size(rrv)));
rdx = [rd00x; rd15x];
rdy = [rd00y; rd15y];
Second, use axis equal, not axis square. There is a difference in the result.
See if these changes do what you want.
I am not sure what I am doing wrong. When I use the code below, I still see the cartesian axes, and the polar plot i cant seem to get right. Do you see any errors in my code? You could just replace my data with the rand functions you have and see my dilema
close all
data=xlsread('C:\Users\Ben\Desktop\Publications\SiC\PKA1_XY Carbon.xlsx','Directions_C_Vac');
data2=xlsread('C:\Users\Ben\Desktop\Publications\SiC\PKA1_XY Carbon.xlsx','Directions_Si_Vac');
tau = 0.5;
rlim = 400;
r = linspace(eps, rlim, 500);
phi = 0.87*sin((log(r)*pi)/(log(tau)));
phi_shifted_45 = 0.87*sin((log(r)*pi)/(log(tau)))+0.78;
rc = 1.5*ones(1, 50); % ‘Round’ The Connecting Line
phic = linspace(phi(end), phi_shifted_45(end), 50);
[xc,yc] = pol2cart(phic, rc);
cfull = linspace(0, 2*pi, 1000); % Full Circle Circumference
ccf = @(rd,th) [rd*cos(th); rd*sin(th)]; % Function
cc15 = ccf(rlim,cfull); % R = 1.5
cc10 = ccf(1,cfull); % R = 1.0
cc05 = ccf(0.5,cfull); % R = 0.5
theta = data(1,:); % Create Data (Angles)
Data1 = data(11,:); % Create Data (First Radius)
Data2 = data(12,:); % 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)], 'b', 'EdgeColor','b') % Fill Area Between Radius Limits
hold on
plot(x1, y1, '-b')
plot(x2, y2, '-b')
axis equal
plot(cc15(1,:), cc15(2,:), 'k') % Plot Full Circumference
plot(cc10(1,:), cc10(2,:), 'k')
plot(cc05(1,:), cc05(2,:), 'k')
plot(rdx, rdy, 'k') % Plot Radials
hold off
axis([-1 1 -1 1]*1.8)
axis square
set(gca, 'XColor','none', 'YColor','none')
Please see my previous Comment. I believe I already addressed your concerns there.
With those changes, your plot should be correct.
Thanks for your help, still struggling to get what I need:
The following code is not working. Did I miss something?
close all
data=xlsread('C:\Users\Ben\Desktop\Publications\SiC\PKA1_XY Carbon.xlsx','Directions_C_Vac');
data2=xlsread('C:\Users\Ben\Desktop\Publications\SiC\PKA1_XY Carbon.xlsx','Directions_Si_Vac');
tau = 0.5;
rlim = 50;
rrv = [0:30:360]*pi/180; % Calculate Radials
[rd00x, rd00y] = pol2cart(rrv, zeros(size(rrv)));
[rd15x, rd15y] = pol2cart(rrv, rlim*ones(size(rrv)));
rdx = [rd00x; rd15x];
rdy = [rd00y; rd15y];
r = linspace(eps, rlim, 500);
phi = 0.87*sin((log(r)*pi)/(log(tau)));
phi_shifted_45 = 0.87*sin((log(r)*pi)/(log(tau)))+0.78;
rc = 1.5*ones(1, 50); % ‘Round’ The Connecting Line
phic = linspace(phi(end), phi_shifted_45(end), 50);
[xc,yc] = pol2cart(phic, rc);
cfull = linspace(0, 2*pi, 1000); % Full Circle Circumference
ccf = @(rd,th) [rd*cos(th); rd*sin(th)]; % Function
cc50 = ccf(50,cfull); % R = 50
cc30 = ccf(30,cfull); % R = 30
cc10 = ccf(10,cfull); % R = 10
theta = data(1,:); % Create Data (Angles)
Data1 = data(11,:); % Create Data (First Radius)
Data2 = data(12,:); % 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)], 'b', 'EdgeColor','b') % Fill Area Between Radius Limits
hold on
plot(x1, y1, '-b')
plot(x2, y2, '-b')
axis equal
plot(cc50(1,:), cc50(2,:), 'k') % Plot Full Circumference
plot(cc30(1,:), cc30(2,:), 'k')
plot(cc10(1,:), cc10(2,:), 'k')
plot(rdx, rdy, 'k') % Plot Radials
hold off
axis([-1 1 -1 1]*1.8)
axis equal
set(gca, 'XColor','none', 'YColor','none')
My pleasure.
I don’t have your data. I also don’t know what ‘not working’ means.
My intent in my code was that you do this:
rlim = 50;
rrv = [0:30:360]*pi/180; % Calculate Radials
[rd00x, rd00y] = pol2cart(rrv, zeros(size(rrv)));
[rd15x, rd15y] = pol2cart(rrv, rlim*ones(size(rrv)));
rdx = [rd00x; rd15x];
rdy = [rd00y; rd15y];
cfull = linspace(0, 2*pi, 1000); % Full Circle Circumference
ccf = @(rd,th) [rd*cos(th); rd*sin(th)]; % Function
cc50 = ccf(50,cfull); % R = 50
cc30 = ccf(30,cfull); % R = 30
cc10 = ccf(10,cfull); % R = 10
theta = linspace(0, 2*pi, 18); % Create Data (Angles)
Data1 = rand(1, 18)*10 + 40; % Create Data (First Radius)
Data2 = rand(1, 18)*10 + 20; % 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')
plot(cc50(1,:), cc50(2,:), 'k') % Plot Full Circumference
plot(cc30(1,:), cc30(2,:), 'k')
plot(cc10(1,:), cc10(2,:), 'k')
plot(rdx, rdy, 'k') % Plot Radials
hold off
axis equal
set(gca, 'Box','off', 'XTick',[], 'YTick',[], 'XColor','none', 'YColor','none')
Change ‘rlim’ to the value you need for your data, and the first arguments in ‘cc50’ and the others to reflect where you want the concentric grid lines. The data in your posted plot go to a radius of about 45, so you may not need to change anything else. Simply substitute your data for the data I created.
I copied down your code, and unfortunately, it just is not working for me. I could try sending you my data, but I don't want to presume to take up to much of your time.
Your data will help. I would have preferred to have had them from the beginning.
I have no idea what ‘not working’ means.
I attached my data. I need the data in row 11 and 12. Note the theta values are in row 1
Your data integrates seamlessly with my code.
The Code
[D,S] = xlsread('Benjamin Cowen example.csv');
theta = D(1,:);
Data2 = D(11,:);
Data1 = D(12,:);
rlim = 50;
rrv = [0:30:360]*pi/180; % Calculate Radials
[rd00x, rd00y] = pol2cart(rrv, zeros(size(rrv)));
[rd15x, rd15y] = pol2cart(rrv, rlim*ones(size(rrv)));
rdx = [rd00x; rd15x];
rdy = [rd00y; rd15y];
cfull = linspace(0, 2*pi, 1000); % Full Circle Circumference
ccf = @(rd,th) [rd*cos(th); rd*sin(th)]; % Function
cc50 = ccf(50,cfull); % R = 50
cc30 = ccf(30,cfull); % R = 30
cc10 = ccf(10,cfull); % R = 10
[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')
plot(cc50(1,:), cc50(2,:), 'k') % Plot Full Circumference
plot(cc30(1,:), cc30(2,:), 'k')
plot(cc10(1,:), cc10(2,:), 'k')
plot(rdx, rdy, 'k') % Plot Radials
hold off
axis equal
set(gca, 'XTick',[], 'YTick',[], 'XColor','none', 'YColor','none')
text([10 30 50], [0 0 0], {'10', '30', '50'}, 'VerticalAlignment','bottom', 'HorizontalAlignment','right')
The Plot
Normally, when I use polar plots, I use the following to set up my axes. How can I do that here? I changed the grid lines to be what I need, but how do I add my labels?
ax = gca;
ax.ThetaAxis.TickLabelInterpreter = 'latex';
ax.ThetaTickLabel = {'[100]','','','[110]','','','[010]','','','[$\overline{1}$10]','','','[$\overline{1}$00]','','','[$\overline{1}\hspace*{0.2mm}\overline{1}$0]','','','[0$\overline{1}$0]','','','[1$\overline{1}$0]','','','[$\overline{1}$ $\overline{1}$0]'};
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.
If my Answer helped you solve your problem, please Accept it!

Connectez-vous pour commenter.

Plus de réponses (2)

Nate Roberts
Nate Roberts le 27 Oct 2021
Modifié(e) : Nate Roberts le 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 commentaire

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.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 18 Mai 2017

0 votes

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 commentaires

Benjamin Cowen
Benjamin Cowen le 18 Mai 2017
Modifié(e) : Walter Roberson le 18 Mai 2017
I've seen similar questions with answers that seemed to work for various people. I just could not figure out how to make it work for mine. I am guessing there is a way. This thread seems to accomplish a similar task but I can't figure out how to implement it for mine. https://www.mathworks.com/matlabcentral/answers/325599-fill-area-between-two-polar-curves
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.
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Polar Plots dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by