Swapping x & y Axis in Matlab - matlab

Potentially easy matlab question here, but I've searched and can't sort out how to do this.
I've got a variables, which plot like this:
I simple want the x axis to be the y axis and vice versa. How do I swap them?
Thank you in advance for your help!!

The standard way would be to swap the arguments passed to plot:
plot(ydata, xdata) %// instead of plot(xdata, ydata)
Failing that, you can change the view to rotate the axes:
view([90 -90]) %// instead of normal view, which is view([0 90])

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

Switch axes on .fig matlab

While editing a .fig matlab file, I want to swap the x and y axes without redrawing the plot by code (without changing the order of vector data). Does a solution exist to my problem? Is there some option in the figure properties menu? Thanks in advance.
In addition to Luis 's answer you can set the current axes View property to [90 -90] directly from the property inspector.
Programmatically this is equivalent to this:
set(gca,'View',[90 -90])
Note:
Thanks to Luis for the correction. Using [-90 90] does swap the axis but then you need to reverse the direction of the y-axis. Therefore it's better to use [90 -90].
Simple example:
Before swap:
And then after changing the view:
You can directly access the 'XData' and 'YData' properties of each plot and swap them:
c = get(gca,'children'); %// get children of axes
for n = 1:numel(children); %// for each children
set(c(n),'XData',get(c(n),'YData'),'YData',get(c(n),'XData')); %// swap XData, YData
end

Distance between axis number and axis in MATLAB figure

I struggle a little bit with overlapping axis numbers of the y and x axis like it is shown in the image. I'd like to keep the size of the numbers and therefore think that simply shifting the numbers away from the axis itself would be an appropriate way to handle this issue.
Is there a possibility to do that?
Thanks in advance,
Joe
Here is a little workaround using text annotations. Basically you clear the current XTick labels and replace them with similar labels, but you can specify the distance from the axis:
clc
clear
close all
x = 1:20;
hPlot = plot(x,sin(x));
set(gca,'xaxisLocation','top');
set(gca,'XTickLabel',[]); %// Clear current XTickLabel
ylim = get(gca,'YLim'); %// Get y limit of the plot to place your text annotations.
for k = 2:2:20
text(k,ylim(2)+0.1,num2str(k),'HorizontalAlignment','Center') %// Play with the 'ylim(1) -0.1' to place the label as you wish.
end
Giving this:
Of course now it's exaggerated and you can do the same for the y axis if you want (using the 'XLim' property of the current axis,gca).

How to add arrows to line plots in Matlab?

I would like to add arrows to a plot of a line in Matlab to illustrate flow. The arrows would ideally be on the line pointing in the direction of the line. Is this possible?
In order to draw an arrow in Matlab, use the file exchange free package called arrow.m
Another way is to use great submission on FileExchange - ARROW.M
See also other related questions on SO:
How do I display an arrow positioned
at a specific angle in MATLAB?
How to add a pointer to a 3-D plot
in MATLAB?
The quiver function should be able to do what you want. However, you'll have to compute the direction of the arrow yourself.
Something along the lines of this is ugly but should get you started (but you probably want to normalize the direction vector to get a nicer graph)
plot(x,y)
hold on
quiver(x(1:end-1), y(1:end-1), ones(len(x)-1,1), y(2:end) - y(1:end-1))
If I understood correctly, you are trying to view a vector field?
If that's the case, here is a working example:
%# function: f(x,y)=x^3-2y^2-3x over x=[-2,2], y=[-1,1]
[X Y] = meshgrid(-2:.1:2, -1:.1:1);
Z = X.^3 -2*Y.^2 -3*X;
%# gradient of f
[dX dY] = gradient(Z, .1, .1);
%# plot the vector field and contour levels
figure, hold on
quiver(X, Y, dX, dY)
contour(X, Y, Z, 10)
axis equal, axis([-2 2 -1 1])
hold off
%# plot surface
figure, surfc(X, Y, Z)
view(3)
You can add an arrow to a figure by using the function annotation.
Note that the parent of the object is not the axes, but the figure window iteslf (meaning that when you zoom in/out, the arrow keeps on the same location in the figure window).

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 :)