MATLAB - fill ezpolar plot - matlab

I just have a brief question regarding MatLab.
Say that we have the equation:
r^2 = 2 sin(5t)
I know that I can fill a polar plot by writing, say:
t = linspace(0,2*pi,200);
r = sqrt(abs(2*sin(5*t)));
x = r.*cos(t);
y = r.*sin(t);
fill(x,y,'k')
But say I use the ezpolar instead by giving the equation above a function handle and then typing:
ezpolar(function handle)
Is there any way I can then fill this polar plot? Or do I have to use the procedure outlined above?
Any tips/help will be greatly appreciated!

You can use ezpolar, then modify the resulting figure. If you look at the returned handle from ezpolar, you'll see it is the line itself drawn in the axis. The points from that line object can be extracted, then used to lay a new polygon on top of the same axis. The benefit is, you get to keep all the nice polar lables.
h=ezpolar('sqrt(abs(2*sin(5*t)))')
hold on;
fill(get(h, 'XData'), get(h, 'YData'), 'k');

Related

Matlab Polarplot() and surface color

I am a bit struggling with my polar plot. I am playing with strikes and dips, and for each pair of those, an "intensity". I'd like to plot this surface/contourf/whatever function on my polarplot. I cannot find the handle to do so. Dpp2 contains the intensity value for a given theta and rho/ strike and dip.
xTmp = (0:4:360);
yTmp = (0:22.5:90);
[strike,dip]= meshgrid(deg2rad(xTmp),deg2rad(yTmp));
dip2 = rad2deg(dip);
strike2 =rad2deg(strike);
figure('name', 'COLD');
polarplot([0 360],[0 90]);
s = surf(strike2, dip2, DPp2);
polarplot(s);
colormap
I've tried something like that, which obviously doesn't work.
cheers,
Flo
As far as I know there is no way of creating a surface plot directly in a polarplot.
One workaround is to manually create your polar axis plot. You can find an example here.
Another workaround would be to use
polarscatter to create a scatter plot (which looks simmilar in case you have a tight grid) Have a look at this.
Because you mentioned the handle: In case you want a handle to the axes have a look at polaraxes from here.
The polar scatter wasn't working, so I tried another function, which seems to work according to this page: https://fr.mathworks.com/matlabcentral/answers/95796-how-do-i-create-a-contour-plot-in-polar-coordinates
I am note quite there yet, the contour map isn't "wrapped" around my polar plot, but so far it's compiling. If anyone has an idea on how to superimpose the contour map onto the polar plot ?
dip2 = rad2deg(dip);
strike2 =rad2deg(strike);
h = polar([0 360],[0 90]);
hold on;
contourf(strike2,dip2,DPp2);
% Hide the POLAR function data and leave annotations
set(h,'Visible','off')
% Turn off axes and set square aspect ratio
axis off
axis image

Choosing points from Matlab contour function

I was wondering how I can view point values off a contour plot.
I wish to obtain all the x-positions at the initial time, t=0 from my contour plot. Can this be done?
I have read on this forum about Choosing isolines from Matlab contour function however I wish to get the actual points.
Any suggestions is much appreciated.
Untested, but recall it to be something like this:
h = findobj(gcf,'Type','Line');
x = get(h,'XData');
y = get(h,'YData');

Polar gridlines on a Cartesian scatter plot

I have a script to create scatter plots (using gscatter) based on x-y data (discrete data points, not continuous) produced by another script. Since these data points are actually the locations of certain objects in a circular space, adding polar grid lines will make the plots more meaningful.
Does anyone know how to show polar grid lines on a Cartesian scatter plot, or am I better off using polar plots?
I once made this script for drawing a polar coordinate system on top of a regular plot. Perhaps it can be useful for you. It is based on this script but simplified to only draw the coordinate system and no data. If this wasn't what you were looking for, check out the linked script, perhaps it can help as well.
Be sure to tweak the radius as needed! I usually disable the axis but it's up to you to fix that if you need another look :)
R=6000; %radius
S=10; %num circ.lines
N=10; %num ang.lines
sect_width=2*pi/N;
offset_angle=0:sect_width:2*pi-sect_width;
%------------------
r=linspace(0,R,S+1);
w=0:.01:2*pi;
clf %remove if needed
hold on
axis equal
for n=2:length(r)
plot(real(r(n)*exp(j*w)),imag(r(n)*exp(j*w)),'k--')
end
for n=1:length(offset_angle)
plot(real([0 R]*exp(j*offset_angle(n))),imag([0 R]*exp(j*offset_angle(n))),'k-')
end
%------------------
You can always use the pol2cart function to generate the polar grid lines.
For example:
function DrawGridLines
x = randn(10);
y = randn(10);
figure;scatter(x(:),y(:));
hold on ;
for angle = 0:20:(360-20)
[x1,y1] = pol2cart( angle / 180 * pi , [0 2]);
plot(x1,y1,'r')
end
for rho = 0:0.1:2
[x1,y1] = pol2cart( 0:0.01:2*pi , rho);
plot(x1,y1,'b')
end
axis equal
end

isosurface function MATLAB usage

Hi can any one give me a simple example of how to use the isosurface function in MATLAB.
The example given if you type help isosurface is quite confusing. Searching on google did not help as no one gives simple examples anywhere. All of them use predefined functions like
flow.
For starters, suppose i have points (x,y,z) where z=0 and at each point I define a constant
function f(x,y,z)=6. So if I use the isosurface function on the isovalue 6 I would like MATLAB to give me a 3d plot with the XY plane highlighted in some colour, say green.
I don't quite understand your example, but here's how you use isosurface to draw a sphere:
%# create coordinates
[xx,yy,zz] = meshgrid(-15:15,-15:15,-15:15);
%# calculate distance from center of the cube
rr = sqrt(xx.^2 + yy.^2 + zz.^2);
%# create the isosurface by thresholding at a iso-value of 10
isosurface(xx,yy,zz,rr,10);
%# make sure it will look like a sphere
axis equal
The example you gave is very uninteresting, in fact maybe even problematic.
By collapsing all points to z=0, you no longer can/need to use ISOSURFACE, and CONTOUR should be called instead. Even then, a constant function f(X,Y)=6 won't show anything either...
Since #Jonas already showed how to use ISOSURFACE, here is an example for the CONTOUR function:
%# create a function to apply to all X/Y coordinates
[X,Y] = meshgrid(-2:0.1:2,-1:0.1:1);
f = #(X,Y) X.^3 -2*Y.^2 -3*X;
%# plot the function surface
subplot(121), surfc(X,Y,f(X,Y))
axis equal, daspect([1 1 3])
%# plot the iso-contour corresponding to where f=-1
subplot(122), contour(X,Y,f(X,Y),[-1 -1]),
axis square, title('Contour where f(X,Y)=-1')

Fixing the Radial Axis on MATLAB Polar Plots

I'm using polar plots (POLAR(THETA,RHO)) in MATLAB.
Is there an easy way to fix the range for the radial axis to say, 1.5?
I'm looking for something analogous to the xlim, ylim commands for cartesian axes. Haven't found anything in the docs yet.
this worked for me... i wanted the radius range to go to 30, so i first plotted this
polar(0,30,'-k')
hold on
and then plotted what i was actually interested in. this first plotted point is hidden behind the grid marks. just make sure to include
hold off
after your final plotting command.
Here's how I was able to do it.
The MATLAB polar plot (if you look at the Handle Graphics options available) does not have anything like xlim or ylim. However, I realized that the first thing plotted sets the range, so I was able to plot a function with radius range [-.5 .5] on a [-1 1] plot as follows:
theta = linspace(0,2*pi,100);
r = sin(2*theta) .* cos(2*theta);
r_max = 1;
h_fake = polar(theta,r_max*ones(size(theta)));
hold on;
h = polar(theta, r);
set(h_fake, 'Visible', 'Off');
That doesn't look very good and hopefully there's a better way to do it, but it works.
Simple solution is to make a fake graph and set its color to white.
fake=100;
polar(0,fake,'w');
hold on;
real=10;
polar(0,real,'w');
In case anyone else comes across this, here's the solution:
As Scottie T and gnovice pointed out, Matlab basically uses the polar function as an interface for standard plots, but with alot of formatting behind the scenes to make it look polar. Look at the values of the XLim and YLim properties of a polar plot and you'll notice that they are literally the x and y limits of your plot in Cartesian coordinates. So, to set a radius limit, use xlim and ylim, or axis, and be smart about the values you set:
rlim = 10;
axis([-1 1 -1 1]*rlim);
...that's all there is to it. Happy Matlabbing :)