Filter löschen
Filter löschen

How to create a surface plot having 3 vectors?

48 Ansichten (letzte 30 Tage)
Din N
Din N am 20 Apr. 2023
Kommentiert: Star Strider am 15 Mai 2024
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 Kommentare
Matt J
Matt J am 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 am 20 Apr. 2023
maybe it would be worth to share your data and make a sketch of what plot you want

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 20 Apr. 2023
Bearbeitet: John D'Errico am 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.
  2 Kommentare
Sorin
Sorin am 15 Mai 2024
To expand on this question: I have a set of 3 vectors that were generated with points at varying intervals, xdata ydata zdata. I can easily create a plot3 and show those lines, but I want to have a nicer plot that is filled between each surface. The data is one to one, so each x and y uniquely specify a z, i.e. x = [1.5 2 3 4 1.2 2.1 3.1 4.2 1.6 1.9 3.3 4.4], y = [1,2,3,4 1 2 3 4 1 2 3 4] (add some slight +- 0.1 variation), z = [1 2 3 4 5 6 7 8 9 10 11 12].
I basically want to plot this nifty thing as a surface:
Star Strider
Star Strider am 15 Mai 2024
Your vectors apparently don’t actually describe the waterfall plot you posted.
If you can provide those vectors (even as matrices), using this approach would probably work —
x = [1.5 2 3 4 1.2 2.1 3.1 4.2 1.6 1.9 3.3 4.4];
y = [1,2,3,4 1 2 3 4 1 2 3 4]; % (add some slight +- 0.1 variation),
z = [1 2 3 4 5 6 7 8 9 10 11 12];
figure
stem3(x, y, z)
hold on
scatter3(x, y, z, 50, z, 'filled')
hold off
colormap(turbo)
F = scatteredInterpolant(x(:), y(:), z(:));
xv = linspace(min(x), max(x), 50);
yv = linspace(min(y), max(y), 50);
[X,Y] = ndgrid(xv, yv);
Z = F(X,Y);
figure
surfc(X, Y, Z)
colormap(turbo)
If the points were actually colinear, scatteredInterpolant would throw an error.
.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by