how to plot 3d using patch?
33 次查看(过去 30 天)
显示 更早的评论
i have a example code. but i don't know how to adapt this code to my data.
my data is longitude, latitude, altitude data(aircraft data).
i drew myself what i want to plot for you conveniene.(first image and second image) % ignore red point in box
first image is a cross section of trajectory. and I want to make somethig like a tube with this data.
and i have to make a box using 'percentile' from cross section and connect i th box's vertices to i+1 th box's vertices, so i can make something like a tube(second image).
I annotated what i don't understand on example code.
let me know which code do i need to change for my output!
thanks!
% Example Code
x = rand(1,5000)*10+125;
y = randn(1,5000)*50+250;
z = randn(1,5000)*150+300;
t = linspace(0, 1, 5000); % why is 't' needed?
x = x + sin(2.5*pi*t)*125;
y = y + cos(1.5*pi*t)*125;
yv = linspace(min(y), max(y), 7); % Set 'Y' Values For The Box Locations
figure
scatter3(x,y,z,'.')
hold on
for k = 1:numel(yv)
yrng = find(y>=0.8*yv(k) & y <=1.2*yv(k)); % what do 0.8 and 1.2 mean? and why do i need to use this code?
xpctl = prctile(x(yrng),[2.5 97.5]);
zpctl = prctile(z(yrng),[2.5 97.5]);
xl(k,:) = xpctl;
zl(k,:) = zpctl;
patch([xpctl flip(xpctl)], [0 1 1 0]+yv(k), [[1 1]*zpctl(1) [1 1]*zpctl(2)], 'r', 'FaceAlpha',0.25)
% I don't know why fllip 'xptcl' and why plus yv(K)
end
plot3(xl(:,1), yv(:), zl(:,1), '-k', 'LineWidth',2)
plot3(xl(:,1), yv(:), zl(:,2), '-k', 'LineWidth',2)
plot3(xl(:,2), yv(:), zl(:,1), '-k', 'LineWidth',2)
plot3(xl(:,2), yv(:), zl(:,2), '-k', 'LineWidth',2)
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
% xlim([120 140])
% ylim([100 400])
view(45,30)
% My code
for i = 1:length(33L)
x = 33L(i).Longitude;
y = 33L(i).Latitude;
z = 33L(i).BAlt;
t = linspace(0, 1, 5000);
yv = linspace(min(y), max(y), 30); % Set 'Y' Values For The Box Locations
scatter(x,y,z,'.')
hold on
end
for k = 1:numel(yv)
yrng = find(y>=0.8*yv(k) & y <=1.2*yv(k));
xpctl = prctile(x(yrng),[2.5 97.5]);
zpctl = prctile(z(yrng),[2.5 97.5]);
xl(k,:) = xpctl;
zl(k,:) = zpctl;
patch([xpctl flip(xpctl)], [0 1 1 0]+yv(k), [[1 1]*zpctl(1) [1 1]*zpctl(2)], 'r', 'FaceAlpha',0.25)
end
plot3(xl(:,1), yv(:), zl(:,1), '-k', 'LineWidth',2)
plot3(xl(:,1), yv(:), zl(:,2), '-k', 'LineWidth',2)
plot3(xl(:,2), yv(:), zl(:,1), '-k', 'LineWidth',2)
plot3(xl(:,2), yv(:), zl(:,2), '-k', 'LineWidth',2)
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1012805/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1012810/image.png)
2 个评论
KSSV
2022-5-27
Does code run in the first place?
for i = 1:length(33L)
x = 33L(i).Longitude;
y = 33L(i).Latitude;
z = 33L(i).BAlt;
The above lines violate MATLAB rules.
采纳的回答
Prahlad Gowtham Katte
2022-6-8
Hello,
As per my understanding of the query ,you want to use the example code for plotting a 3d graph using patch function but wants to know why certain things in the code is used.
Firstly “linspace” function gives equidistant numbers within specified range which is used later in the code for getting sample points of x and y. “Find ”function is used for finding the indices of the array which satisfy a specified condition.It is used in the example code for getting indices of y values which is greater than 80% of y and less than 120% of y. These are later used to get the percentile required for using “patch” function.
Flip function is used to invert the order of x percentile array in order to get a rectangular surface. We add “yv(k)” to the array because we need to get the box around y=yv(k).Thus we add “yv(k)” to the initial array.
For more information on the functions used in the code please refer to the following links.
Hope it helps!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!