Matching axes scales - matlab

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.

Related

How to change axis limits and tick step of a MatLab figure?

I have a simple plot of y against x.
y = [6,-1.3,-8,-11.7,-11,-6,1.3,8,11.7,11,6,-1.3];
x = 0:0.3:3.3;
plot (x,y)
As the result, the x-axis of the figure is ranging from 0 to 3.5, with scale of 0.5. I have used the XLimit = [0 3.3] to limit the axis, but it seems like not working.
I wish to make the x-axis range from 0 to 3.3 with steps of 0.3.
axis tight % removes the empty space after 3.3
set(gca,'XTick',0:0.3:3.3) % sets the x axis ticks
With XLimit = [0 3.3] you just define a vector called XLimit. In order to use this vector as horizontal limit, you should use xlim:
xlim(XLimit)
% or directly:
xlim([0, 3.3])
Read more about xlim here. Similarly you can set the vertical limit with ylim.
Since you are trying to set the limits equal to the range of x, you will probably find the following command most helpful:
axis tight
But note that it changes both x- and y-axis limits.
To set the tick step, as AVK said, you should set the 'XTick' to 0:0.3:3.3:
set(gca,'XTick',0:0.3:3.3)
gca is the handle to current axes.

Matlab: Image plotting resolution/dpi

I am plotting x and y coordinates together with some images but the images are becoming extremely large compared to the axis. How can i set the figure resolution or size so that an image can fit in a small axis range rather than covering the whole graph and also modify the axis range.
Figure without images (below).
Matlab code:
for l = 1:size of array
colormap('gray');
imagesc(X,Y, imrotate(imresize(img,[100 100]),180));
end
By default, imagesc will plot the image using indexed coordinates (pixel spacing of 1). Note the axes limits on this figure:
You can change this by altering the XData and YData properties of the image.
him = imagesc(rand(4), 'XData', [0,1], 'YData', [0 1])
Notice that this changed the scaling of your image. I'm not sure what coordinate system your points are (and if you can provide a little more information I can help you better), but you can adjust the XData and YData to be the appropriate values to match your points.

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:

"Density" plots in Matlab, but not in the sense of density of data points

I would like to plot a sort of "density map" in Matlab, but have not found the right tool yet.
I have "continuous" data with x between (x_min and x_max), and y between (y_min and y_max). At each of these pairs of points (x_i,y_i), there is associated to it a value between 0 and 1.
I would like to plot this information in a 2d graph, such that in each small square containing (x_i,y_i) the plot shades the square black for the value 0, white for the value 1, and the appropriate shade of gray for intermediate values.
Can this be done easily in Matlab?
http://www.mathworks.com/help/images/ref/mat2gray.html seems to do exactly what I need.
If the data is in a matrix A, you can just use
image(255*A); colormap gray(256); axis image;
I'm not sure what you mean by continuous (uniformly spaced?), so my answer won't make too many assumptions other than that there is a reason why you mention the coordinates (if just a regular mesh, then just image or imagesc). So, only assuming your x and y coordinates are possibly non-uniformly spaced, but at least monotonically increasing samples, try surf with view(2):
surf(X,Y,data)
view(2)
colormap gray
By default surf sets the FaceColor property with the 'flat' option:
flat — The values of CData determine the color for each face of the surface. The color data at the first vertex determine the color of the entire face.
In other words, the value will determine the shade.
Assuming your data is in data and your x and y coordinates are in x and y, here is how to do it:
imagesc(x, y, data) % to create a heat map
colormap(gray) % for gray levels
caxis([0 1]) % to set 0 to black and 1 to white
axis xy % if you want the y axis to point up
colorbar % to display the colorbar

multiple axis 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.