How to create a surface plot having 3 vectors?

63 views (last 30 days)
Din N
Din N on 20 Apr 2023
Edited: John D'Errico on 20 Apr 2023
I have 3 vectors X, Y, Z and each vector is 1x200.
I need to show the relationship between them on a plot. I used "plot3" which works, but it doesn't show the relationship that well.
So, I was wondering if there is a way to create a surface plot. I tried to do:
surf(X,Y,Z) but it does not work and Matlab wants Z to be a matrix where Z depends on X and Y which is not the case in my calculations.
So, is there a way to get a surface plot with X, Y, and Z?
P.S.: I looked at other questions like this, but I am still confused how their answers can be applied to my case.
  2 Comments
Matt J
Matt J on 20 Apr 2023
It is not clear in what way the line plot from plot3 is insufficient, and why a surface would be better, even if you could define one.
Mathieu NOE
Mathieu NOE on 20 Apr 2023
maybe it would be worth to share your data and make a sketch of what plot you want

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 20 Apr 2023
Edited: John D'Errico on 20 Apr 2023
Without knowing what your data looks like, it is difficult to help. And we don't see any data.
For example, do you think that surf should somehow automatically know how to connect these points into a closed surface? The problem is, surf cannot understand the connectivity of a random set of data. What are the relationships between those points? Should the result be a nice smooth surface? How can it possibly know? For example, you tell us that you have 200 points. Do they fall on a simple path in 3-d? Do they fall on the surface of sphere? Do they fall on some nice simple surface, that otherwise has some functional relationship? For example, here are several examples, each of whoich would be completely different in what surf might be expected to do.
t = linspace(0,4*pi);X = t.*cos(t);Y = 2*t.*sin(t);Z = t;
plot3(X,Y,Z,'o')
box on
grid on
X = rand(200,1);Y = rand(200,1);Z = exp(X + Y) + randn(size(X));
plot3(X,Y,Z,'o')
box on
grid on
XYZ = randn(200,3);
XYZ = normalize(XYZ,2,'norm');
X = XYZ(:,1);Y = XYZ(:,2);Z = XYZ(:,3);
plot3(X,Y,Z,'o')
box on
grid on
The first set of points falls on a conical helical spiral. We could probably decide it forms a surface. Or not. How should surf know?
Then we have a simple, but noisy surface, of the form Z=exp(X+Y), but with a fair amount of noise added on. Should surf somehow be able to decipher that?
Finally, a set of points that lie on the surface of a unit sphere.
Do you want me to keep on going? I can surely cook up many different examples of "surfaces" that would confuse surf.
How should surf somehow magically be able to look at each set of points, and know what you would expect to see as a result? In fact, each of those problems should be handled completely differently. But without any kind of intelligence to guide it, surf can do nothing more than give up. It is you who needs to first decide what is the connectivity of those points, and then form it into something that surf is designed to handle. The onus is on you here, the user.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!