multiple axis matlab - matlab

I have three variables:
First variable is: time (datenum),
Second variable is: depth,
Third variable is: u (x component of velocity)
I need to plot the u with x axis as time, u data should be starting from that depth.
I tried to use this:
x1 = time;
y1 = depth(:,1);
y2 = u(:,1);
plotyy(x1,y2,x1,y1);
But i dont want to plot depth but instead i want the u data to start from that depth value, but depth value should be shown on second y axis. Since i will be changing the depth matrix again and plotting on same plot.
Note that depth matrix is just one depth (1.20) through out.

If I understand what you're saying here, you're looking to plot time alog the X axis and depth along the Y axis (which you already have).
But the caveat is that you want to start te Y axis from a certain depth that you have in the variable 'u'.
To do this you might want to look into the axis command in matlab:
This will allows you to set Xmin, Xmax, Ymin and Ymax and scale the plot how you like.

Related

Matlab Surface Plot

I have a set of data points, x, y, and z contained in a matrix, record.
In record, each row is a data-point where the first value is the x-coordinate, the second is the y-coordinate, and the third is the z-coordinate. I would like to represent this as a surface plot. I tried:
surf([record(:,1), record(:,2)], record(:,3))
But the results were not what I expected. Any advice?
Try this code for instance.
[x,y,z]=sphere(n);
surf(x,y,z);
axis equal
This code plots with 3 parameters surf the surface of a sphere. As far as I understood from your code you want to utilize the 2 parameters surf for your application.
According to surf help when utilizing 2 parameters surf:
surf(Z) and surf(Z,C) use x = 1:n and y = 1:m. In this case,
the height, Z, is a single-valued function, defined over a
geometrically rectangular grid.
where:
The color scaling is determined
by the range of C
It just doesn't look like you want to utilize C as the color scaling parameter. For better understanding, can you send the contents of record for reference?

Changing perspective of Matlab plots

Say I have a matrix hey 15x15. I want to plot the value of the matrix as a 2D plot for better visualization. But Matlab plots with the convention that origin is in bottom-left corner and positive x is along the left and positive y is along the up direction from origin.
but i want to make my plots such that origin is in top-left corner, +ve x is left and +ve y is down.
So i just used a slight trick.
figure
axis([0 15 -15 0]);
daspect([1,1,1])
hold on
rectangle('Position',[3,-6,2,3],...
'EdgeColor','black',...
'LineWidth',2,...
'FaceColor','cyan')
for i=1:nrows
for j=1:ncolumns
if char(hey(i,j))=='^'
text(j,-i,'^');
elseif char(hey(i,j))=='>'
text(j,-i,'>');
elseif char(hey(i,j))=='v'
text(j,-i,'v');
elseif char(hey(i,j))=='<'
text(j,-i,'<');
end
if obstacle(i,j)==1
text(j,-i,'X');
end
end
end
text(goalY,-goalX,'T');
I made the transformation (x,y)-->(y,-x). But the downside is that the axes are then numbered along y as -1 to -15. However if reader was following above, i only wanted to plot the matrix values and in matrix the y runs +ve downwards from 1 to 15 for my case.
So i want the plot to show +1 thru +15 along y with origin at top-left and x graduated as it is but the values +1 to +15 written at the top of the plot rather than below.
How to do this? In the extreme case, i am alos willing to transfer the matrix hey to another software that can do the nice plot as i want. If any of the two alternatives is possible, please give concrete steps to do it.
EDIT:
After using the helpful methods below, i still have to use a trick like plot (j,i) instead of the innocent plot(i,j). This is because for matrix (i,j) is mapped to graph plot (x,y) as x=j, y=i. Is there a similar workaround? a matrix element is (row #, column #). But in 2D matlab graph, we will denote it's position as (column #, row #). I was just guessing if there was some matlab in-built function to take care of this. like i will give it (row #, column #) but matlab will plot (column #, row #). Is there such a function?
I think axis ij does what you want:
axis ij places the coordinate system origin in the upper left corner. The i-axis is vertical, with values increasing from top to bottom. The j-axis is horizontal with values increasing from left to right.
To locate the x axis on top, change the 'XAxisLocation' of the axes to 'top' (default is 'bottom').
Example:
x = 1:10;
y = x.^2;
plot(x,y)
axis ij
set(gca,'XAxisLocation','top')
Original plot (lines 1-3 of above code):
After axis ij (line 4):
After set(gca,'XAxisLocation','top') (line 5):
If I followed correctly you are looking for the axes XAxisLocation and YDir properties. You can set them to top and reverse respectively to get the output you want. You can also set the XTick property to 1:15 to show every value from 1 to 15.
Example:
clear
clc
%// Create dummy data
[x,y] = meshgrid(1:15,1:15);
u = cos(x).*y;
v = sin(x).*y;
figure
quiver(x,y,u,v)
set(gca,'XAxisLocation','top','XTick',1:15,'YDir','reverse')
hold on
%// I changed the coordinated of the rectangle to fit with the change in
%y-axis.
rectangle('Position',[3,3,2,3],...
'EdgeColor','black',...
'LineWidth',2,...
'FaceColor','cyan')
axis([0 15 0 15])
Which gives the following:

3D scatter plot with 4D data

I need to plot a 3D figure with each data point colored with the value of a 4th variable using a colormap. Lets say I have 4 variables X,Y,Z and W where W = f(X,Y,Z). I want a 3D plot with X, Y, and Z as the three axis. The statement scatter3(X,Y,Z,'filled','b') gives me a scatter plot in 3D but I want to incorporate the value of Z in the graph by representing the points as an extra parameter (either with different areas :bigger circles for data points with high value of Z and small circles for data points with low value of Z or by plotting the data points with different colors using a colormap). However, I am a novice in MATLAB and dont really know how to proceed. Any help will be highly appreciated.
Thanks in advance!
So just use z for the size vector (4th input) as well as the color vector (5th input):
z = 10*(1:pi/50:10*pi);
y = z.*sin(z/10);
x = z.*cos(z/10);
figure(1)
scatter3(x,y,z,z,z)
view(45,10)
colorbar
The size vector needs to be greater 0, so you may need to adjust your z accordingly.
You are already nearly there... simply use
scatter3(X,Y,Z,s,W);
where s is the point size (scalar, e.g. 3) and W is a vector with your W values.
You might also want to issue an
set(gcf, 'Renderer','OpenGL');
where gcf gets your current figure you are plotting in to significantly increase responsiveness when scattering a lot of data.

Matlab - Multiple 2D plots along a 3rd dimension

I'm trying to plot many 2D plots (x,y).
But...
each 2D plot is for a constant z.
So really my data is (x,y,z) but not z(x,y), which I believe are the requirements for using the "surf" command.
Could anyone help with this?
Example,
x = velocity
y = drag
I have multiple runs of y(x) for a constant temperature, z.
I just want to plot each (x,y) along a 3rd axis, temperature z.
Ideally I'd also want some sort of contour between the (x,y) plots so I can show the peaks/troughs etc.
Any help would be great.
If the runs are not independent (there is some trend over multiple runs) then it may make sense to use surf. You then need to construct your data such you have an X,Y, and Z - in this case I'd suggest you use the drag measurements as your Z (height).
Assuming that you have all the drag/velocity data in drag and velocity which are both of size [data points x number of runs]:
% construct matrix of run numbers
runs = repmat(1:numruns, [1, datapoints]);
runs = reshape(runs, datapoints, numruns);
% plot and label
surf(runs,velocity,drag);
xlabel('runs')
ylabel('velocity')
zlabel('drag')

Matching axes scales

I have 3D data plotted using the 'plot3' function. I would like to constrain the Y and Z axes such that they are equal in scale. The X axis should be automatically scaled as usual.
I know from here that I can make the X axis be the only one to be automatically scaled by using the command:
axis 'auto x';
However, this causes the Y and Z axes to be plotted from 0 to 1 only; my data often exceeds this in all axes. What I'm looking for is a plot which contains all the data in a single view, but with the smallest of the Y or Z axes scaled down so that the Y and Z axes are equivalent in scale.
Try daspect.
plot3(5*rand(10,1),10*rand(10,1),rand(10,1))
tmpAspect=daspect();
daspect(tmpAspect([1 2 2]))
daspect() returns the current aspect ratio as produced by axis 'auto'.
daspect(tmpAspect([1 2 2])) then enforces that y and z have the same scale.
How about
axis equal
or even
axis tight
axis equal
both after the plot has been drawn.
Is this what you mean?
Type help axis at the Matlab command prompt for more capabilities of the axis function.