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 :)
Related
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
I am currently trying to plot 3D streamtubes. I want the tubes to be colored corresponding to their respective velocity (e.g. slow = blue, fast = red).
To be more exact, I have three 3D-matrices containing the velocity in x, y and z direction. The color of the streamtubes should be sqrt(vx^2+vy^2+vz^2). When using streamtube(x,y,z,vx,vy,vz,sx,sy,sz) the tubes are colored according to their z-coordinate which is useless because it's a 3D plot anyway.
Well this wasn't easy (it ought to be a builtin option), but by modifying the CData of each tube (they are each their own graphics object), you can achieve the desired result. Here's an example
load wind
[sx,sy,sz] = meshgrid(80,20:10:50,0:5:15);
h=streamtube(x,y,z,u,v,w,sx,sy,sz);
drawnow
view(3)
axis tight
shading interp;
This gives this picture:
but then doing this:
vel=sqrt(u.^2+v.^2+w.^2); %// calculate velocities
for i=1:length(h)
%// Modify the colour data of each tube
set(h(i),'CData',interp3(x,y,z,vel,get(h(i),'XData')...
,get(h(i),'YData'),get(h(i),'ZData'),'spline'))
end
drawnow
view(3)
axis tight
shading interp;
gives this result
NOTES:
1) I don't know if this is fully correct, I don't know how to test it
2) You have to interpolate the velocity data from the points where it's known onto the vertices of the streamtubes
3) I found the spline interpolation option to work best, but the other options might work better in other cases
To state it simply, how do I map a point in the coordinate system used for plotting to figure coordinates in Matlab?
For a more thorough explanation, consider the following dummy code:
figure(1);
axes('position', [0.1 0.1 0.8 0.8]); % define position of axis in fig coords
hold on;
box on;
xlim([0, 2*pi]);
ylim([-1.1, 1.1]);
% plot something
t = 2*pi*linspace(0,1,100);
plot(t, sin(t));
The limits of the axis are [0, 2*pi] and [-1.1, 1.1]. These are the plotting coordinates I refer to. The position of the axis is defined with figure coordinates.
Let's say I want to find out the figure coordinates of a given point, what should I do? For example, the point [pi, 0] should be at the exact center (at [0.5 0.5]). But what' the figure coordinates of, say, [pi/2, 1]?
I could easily write a routine that works in this specific case using the known limits of the axes, and the position of the axes. I am, however, looking for a more general solution. What if my plot uses logarithmic scales? What if it's a 3D plot? Etc. etc. I would imagine Matlab has a simple routine for this but I haven't been able to find it.
Also, is there a routine for the opposite direction, mapping from figure cooridnates to plotting coordinates?
Edit
What I'm trying to achieve, ultimately, is fancy figures. More specifically I want to put annotations at exact points.
I'm creating a 2d MATLAB plot. I'm setting the limits of my x axis, and letting my y axis auto-adjust (by setting its limits to [-inf inf]). After creating my plot, I need to check what my y axis has auto adjusted to (as I'm going to create a heatmap to put under my plot).
Unfortunately, ylim (and similar functions) only produce [-inf inf], not whatever the axes have adjusted to.
Some code which reproduces this problem (much more simply than my actual code) is:
function createplot(xbounds)
x = xbounds(1):0.5:xbounds(2);
y = x.^2;
plot(x,y);
axis([xbounds,-inf,inf]);
createplot([0,10])
which produces a parabolic plot with y limits = [0,100]. However, ylim = [-inf, inf].
Any help would be appreciated!
/ Wilbur
As #Shai suggested, axis can give info regarding the ylimits without the need to set them to [-inf,inf] or use axis to set the x-axis bounds:
xbounds=[1 10]
x = xbounds(1):0.5:xbounds(2);
y = x.^2;
plot(x,y);
xlim([xbounds(1) xbounds(2)]);
v=axis
v =
1 10 0 100
Looking at #natan's answer I think the solution to your problem is
Do not use [-inf inf] for auto-adjusting axis limits.
If you want Matlab to auto adjust some of your axes limits and manually set others, then you should use either xlim, ylim or zlim for the specific axis you wish to set and leave all the other unchanged so Matlab can set them automatically.
This way you will not override the values Matlab assigns to those axes and you will be able to read them using axis, xlim, ylim or zlim.
Please see #natan's answer for corrected code.
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