Reputation:
I have 2 variables that I'm trying to graph and I can't figure out what functions to use in order to get the desired graph that I want. For example, I have a variable Depth which is a 70x12 matrix full of numbers. 12 is for each month of the year. I'm trying to graph Depth against Temperature which is also a 70x12 matrix full of numbers. The current way I am doing this is by using plot3 in a for loop with hold on and plotting each Depth vs Temperature curve separated by 1 on the z-axis. That looks like this:
And when rotated it looks like this:
However, what I want is some sort of meshgrid or surf inbetween my curves so that my graph would look something similar to this. I played around with surf and mesh a decent bit but I can't figure out how to use the data that I have stored in my variables to plot curves and a surface through the curves that looks anything like that.
Upvotes: 1
Views: 1865
Reputation: 4097
You can do what you want using plot3
and surf
but it is highly advisable to create matrices of both the depth and the temperature and the months using meshgrid.
The following code shows how to do this. I just made up some arbitrary temperature data called temps
and then used meshgrid
to turn the vector of depths and months into a grid corresponding to each entry in temps. Notice that because M
and D
are now matrices I don't need a for
loop and can just plot3
them all at once.
% create some data. The problem is that depths and months are vectors while
% I have a 70x12 grid o temperatures:
nd = 70;
depths = linspace(0, 1e3, nd);
months = 1:12;
temps = sin((1:nd)'*months/(nd*8));
% So make temps and depths matrices too:
[M, D] = meshgrid(months, depths)
% and now plot:
figure(112)
clf
% plot the surface from the matrices returned by meshgrid: notice I used
% facealpha to change the transparency:
hs = surf(temps, D, M, 'facealpha', .5);
hold all;
% now that we used meshgrid we can plot everything at once:
plot3(temps, D, M, 'linewidth', 3)
view(3)
grid on
Upvotes: 0