简体   繁体   中英

Matlab: 3D Surface Plot of values from own vectors

I have 3 vectors with values for coordinates (X,Y,Z) and I want to plot them as a surface. I have tried all sorts of solutions from here and other forums and cannot for the life of me get it to look like anything that makes sense. I have a picture that can describe the situation better but I can't post it as I don't have enough reputation. Please help. Thank you.

EDIT: This is the link to the picture :在此处输入图像描述

In the picture, the origin of each vector arrow represents the point in a 3D coordinate, with the length of the vector arrow giving the magnitude of the required torque to move at that point in 3D space.

Right now, the data is separated in line vectors with each point's coordinate: so that's one vector for the X, one for Y. Z is one line inside a 3 line matrix as the whole matrix describes the required torque in a 3 coordinate axes.

I've tried using meshgrid on the X and Y vectors and then attributed the Z value using griddata and then surf but I'm not getting something that looks like the original:在此处输入图像描述

The plot is linear but I'm pretty sure the data is not... at least not that linear.

It seems like you want want to plot the single points decribed by the vectors, here you have an old answer of mine (as a bonus you will have nice colorful plots 'cause that's what the original question asked):

Assuming Data=[Vec1,Vec2,Vec3,...] and VecN=[Xn,Yn,Zn]'

" If you want to plot points, you can define an RGB color and plot single points with hold on like this:

hold on

for i=1:length(Data(:,1)) 

    plot3(Data(i,1),Data(i,2),Data(i,3),'Color',[(i/100*255)/255 0/255 (255-(i/100*255))/255],'LineWidth',2)

end    

shg

"

Managed to solve the problem. As mentioned, the data was not uniform and because of that, surf was jumping from one end of the plot to the other, creating a total mess. Solved it by organising the values linearly using linspace and then using those values to create the meshgrid and then assign the Z values using griddata with a cubic interpolation.. Managed to produce proper looking surface plots with the data on hand.

Plot each data point separately within a loop:

figure; hold on; grid on;

for i = 1:length(x)
    stem3(x(i),y(i),z(i));
end

Don't forget to add hold on . Use "Rotate 3D" button from the toolbar in the figure window if the plot was shown in 2D at first.

在此处输入图像描述

You can use griddata parameter to natural , cubic or v4 . This will do interpolation and make your graph with non-uniform data smooth Below is a sample MATLAb code

x=[32 20 67 1 98 34 57 65 24 82 47 55 8 51 13 14 18 30 37 39 10 33 21 26 38 81 83 60 95 22 17 5 72 46 99 52 12 25 96 29 70 85 43 69 19 78 97 31 89 53 2 91 48 71 61 15 36 84 94 50 11 80 6 7 49 74 9 88 40 79 27 68 73 64 63 59 86 23 35 58 45 28 100 42 93 87 16 90 41 66 54 92 77 4 62 76 75 56 3 44];
y=[96 75 24 9 83 49 27 77 3 23 17 31 40 13 7 52 51 21 98 47 64 79 78 91 44 16 15 100 84 99 63 68 70 30 54 76 97 73 33 5 88 8 71 66 62 25 60 42 72 45 18 11 28 59 89 65 10 55 69 81 12 26 20 95 87 41 74 50 93 22 43 90 14 34 82 35 56 38 80 32 1 57 6 36 37 61 29 58 2 48 4 46 67 53 92 86 94 19 39 85];
z=[55 31 11 45 83 36 86 49 15 57 42 46 8 94 88 47 54 81 98 41 32 35 56 85 9 89 37 60 23 62 67 100 78 76 73 80 10 20 68 34 77 93 1 63 53 12 22 99 91 40 84 24 33 3 43 19 92 97 6 82 64 25 26 79 95 4 44 58 5 21 70 29 65 87 96 90 51 14 18 2 72 28 71 39 52 7 27 59 50 61 48 30 66 69 17 13 74 16 75 38];

xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
% Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)

matlab_figure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM