Three dimensional plot on matlab - matlab

I am trying to do a figure similar to the one attached.
I have exactly a (224x1) vector with dates (x-axis), a (10x1) vector with maturities (y-axis) and a (224x10) matrix with the values (z-axis).
I tried surf(X, Y, Z) but I got an error ("data dimensions must agree").
How can I combine this to make a plot like the one attached?
Thanks, V!
Edit: The second plot is the one I am getting using Luis Mendo's suggestion:

Use
surf(Y,X,Z)
From the documentation (emphasis added):
surf(x,y,Z) and surf(x,y,Z,C), with two vector arguments replacing
the first two matrix arguments, must have length(x) = nand
length(y) = m where [m,n] = size(Z). In this case, the vertices
of the surface patches are the triples (x(j), y(i), Z(i,j)).
Note that x corresponds to the columns of Z and y corresponds to
the rows.

Do
[X,Y]=meshgrid(x,y);
surf(X,Y,Z);
You need to create a meshgrid to be able to plot a surf. X ,Y and Z need to be the same size!

Related

Dissimilar results to the surface plot in MATLAB

>> x=0:0.001:720;
>> y=sind(x);
>> z=cosd(x);
>> surf(x,y,z);
I want to plot a surface using the above code i.e. x in X-axis, y is Y-axis, and z in Z-axis.
I also programmed a FORTRAN code for the following purpose, created a csv file, and plotted it in origin. I am getting a result as this:
However, in MATLAB, I am getting a similar idea when using:-
>> plot3(x,y,z)
as in this image:
but it's not a surface (for obvious reasons).
When using the surf command, I am also getting an error saying:
Z must be a matrix, not a scalar or vector.
What could be the possible problem with my code?
Using surf requires Z to be a matrix. This is fixed easily with functions like meshgrid (also useful is griddata).
Using meshgrid makes using surf very accessible.
But both Z and Y are just functions of X so I'm unable to explain why your plot Z-value would change with both X and Y. From the analytical (mathematical) equations you've listed, the Z-value should be constant in the Y-dimension.
stepsize = 1; % use 10 for cleaner look
x = 0:stepsize:720;
y = sind(x);
[X,Y] = meshgrid(x,y);
Z = cosd(X);
surf(X,Y,Z)
Notice that the contour lines are straight & parallel in the Y dimension (using surfc(X,Y,Z)).
Another method is to loop through elements of x (indexed by i) and y (indexed by j) where both x and y (vectors) to calculate Z(i,j) where Z is a matrix. Due to default assignment for rows & columns, this method requires transposing the Z matrix such as surf(X,Y,Z.').
Related Posts:
How can I plot a function with two variables in octave or matlab?
MATLAB plot part of surface

putting function values in meshgrid format in matlab

I have coordinate vectors X and Y and corresponding vector of function values Z. I need to plot a surface Z(X,Y), and I need to put Z values on a regular mesh, right now X and Y define points distributed irregularly. What is the best way to do it in matlab?
Thanks!
If you have a function such as z=f(x,y) then you could use fsurf().
fsurf(#(x,y) x^2+y^2);
Or else, if you have data points then you could use surf()
[x, y]=meshgrid(-5:0.1:5);
z = x.^2 + y.^2;
mesh(x,y,z);
You could specify the axis as per your need.
The following worked for me:
Zinter = scatteredInterpolant(Xs,Ys,Z) %defines interpolant for scattered data and allows to calculate values of z for any point

How to create a 2D-matrix out of my data for surf()?

I have a 25000x3-matrix, with each row containing a x-, a y- and a z-value. Now I wanted to do a graphical plot out of these. But for using for example surf(Z) I have to use a mxn-matrix as Z with m equal the size of x and n equal the size of y. How can I reshape the matrix I have to the needed mxn-matrix? The problem is that my x- and y-values are no ints, but floats, so I assume that I have to do a interpolation first. Is that true? My data plotted with plot3 looks like:
The fact that your x- and y- values are not integers is not a problem at all. The real question is: are your (x,y) points forming a grid, or not ?
If your points are forming a grid, then you have to reshape your columns to form m-by-n arrays. You may need to sort your data according to the first, then second column and then use the reshape function.
If your points are not forming a grid, then you will have to make an interpolation. By chance the scatterinterpolant class can nicely help you in doing so.
As you can see, the data you are providing is neither given in a gridded way, nor is the point cloud clean. You could however try to do the following:
Project the point cloud onto the x-y plane
Triangulate those points
Give the points their original z-coordinate back.
Plot the surface using trisurf
Here is a MATLAB code that does this:
%// Generate some points P
[X,Y] = ndgrid(0:30);
P = [X(:), Y(:), X(:).^2+Y(:)];
%%// Here the actual computation starts
[~,I] = unique(P(:,1:2),'rows'); %// Remove points with duplicate (x,y)-coords
P = P(I,:);
T = delaunay(P(:,1),P(:,2)); %// Triangulate the 2D-projection
surface = triangulation(T, P(:,1), P(:,2), P(:,3)); %// Project back to 3D
trisurf(surface); %// Plot
You may want to remove stray points first, though.

Plot x=y plane in MATLAB

I fail to think how should I proceed for plotting x=y plane withing a 2x2x2 space.
I create a 2x2 meshgrid
[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
then I come to define Z - I get stuck
My thoughts go like this
I need to create a 21x21 array for Z ( I use surf function.. and the dimensions of X, Y, Z must match. Right?).
I need to populate only those values of Z that follow x==y
Now for each such point i.e. x==y Z will vary -1:0.1:1 .. Does This require that I iterate again and again on the x==y and keep drawing Z with values from -1:0.1:1?
Am I going the right way about plotting this plane? Kindly help.
You simply need to define X and Z, Y is equal to X by definition:
[X Z] = meshgrid(-1:.1:1,-1:.1:1);
figure;
surf(X,X,Z);xlabel('x');ylabel('y');zlabel('z');
Results with
You are actually trying to do something two dimensional in a 3 dimensional setting.
A bit unintuitive, but that does not mean it can't be done, for example:
[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
Z = zeros(size(X)); % Perhaps you want NaN rather than zeros
idx = X==Y;
Z(idx)=X(idx).^2+Y(idx) % Of course identical to X(idx).^2+X(idx)
surf(Z)

MATLAB: Need to make a 4D plot (3D + Colour/Color)

I need to make a 3D surface where colour will represent the fourth variable. I know "surf" is SIMILAR to what I need, but that's not quite it. Basically, I have the following variables:
t = [1:m]
y = [1:n]
a = [1:o]
These should be the three Cartesian corodinate axes.
I also have a variable S that is of dimensions m x n x o, and is basically the amplitude, a function of the previous three variables (i.e. S = f(t,y,a)). I want this to be represented by colour.
So to summarize, I need a graph of the form (t,y,a,S), where the first three variables are vectors of unequal sizes and the final variable is a multidimensional array whose dimensions are determined by the first three.
Thanks in advance.
SCATTER3 requires x, y and z and other grouping arguments to be equally-sized Nx1 vectors for a single series or NxM matrices for M series.
You have full space 3D data. To make equally-sized coordinate vectors use MESHGRID (or NDGRID) function:
[X, Y, Z] = meshgrid(t, y, a);
Then you can use SCATTER3:
scatter3( X(:), Y(:), Z(:), [], S(:) )
The problem is since it's full space data scatter3 will not be helpful specially if you have a lot of points.
You can probably filter your S variable (something like idx = S > 0), then you can plot filtered data.
If you really need to visualize all the data, look at Volume visualization in MATLAB documentation. I can recommend SLICE function, for example.
EDIT
Here is an example of full 3D space scatter plot for small vectors (m, n, o equal to 5) with S = rand([m,n,o]); scatter3( X(:), Y(:), Z(:), [], S(:), 'filled' )
EDIT 2
From your comments to the other answer I found that you have 32x76050x4 matrix. You can actually plot 2D slice one at a time. you can do it in 2D with IMAGESC function, or in 3D with SLICE function.
Try:
imagesc(S(:,:,k))
where k is a number from 1 to 4 for the 3rd dimension.
Or try
slice(S, [], [], 1:size(S,3))
shading flat
Maybe this user-created plotting routine can help.
Screnshot from the linked page:
I've always used scatter3 for coloring/sizing pixels in 3d space. I believe the signature is:
scatter3(x,y,z, size, color)
The size and color can be scalar or vector of length equal to the coordinates. I usually use either the color or the size to reflect the fourth attribute, depending on what I'm showing. I don't have Matlab on this machine, so forgive me if my memory isn't completely accurate on the usage. "help scatter3" should describe it much better.