rotate the coordinates plotted with plot3 without changing the axes
18 views (last 30 days)
Show older comments
Hi. I need to rotate the plotted curves (3D figure on the left) by 180° as shown in the figure on the right. Is there any efficient method?
Attached is the code.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1236782/image.png)
0 Comments
Accepted Answer
Karim
on 20 Dec 2022
Hi, in the code below I added some comments to demonstrate how you can rotate the curve.
imageArray = imread("fig_A.png");
binaryImage = imageArray;
binaryImage = ~binaryImage;
M1 = function_verification_code(binaryImage);
z = 100;
figure
current_z = z * ones(size(M1, 1), 1);
plot3(M1(:, 2), M1(:, 1), current_z, 'b.')
grid on
xlabel('X');ylabel('Y');zlabel('Z');
view([15 50 30])
title("Original Curve - Figure A")
% adjust the x and x vectors before we apply the rotation
M_rot = M1(:,[2 1]);
% find the mean of the curve in the x-y axis, we will use this point as rotation center
P0 = mean(M_rot,1);
% shift grid to the center for the rotation
M_rot = M_rot - repmat(P0,size(M1, 1),1);
% set up the rotation matrix about the z-axis
Rot = @(x) [cosd(x) -sind(x); sind(x) cosd(x)];
% rotate the shifted grid with 180 degrees
M_rot = (Rot(180) * M_rot')';
% shift the curve back to its own centroid
M_rot = M_rot + repmat(P0,size(M1, 1),1);
figure
current_z = z * ones(size(M_rot, 1), 1);
plot3(M_rot(:,1), M_rot(:,2), current_z, 'b.')
grid on
xlabel('X');ylabel('Y');zlabel('Z');
view([15 50 30])
title("Rotated Curve - Figure A")
3 Comments
Star Strider
on 20 Dec 2022
The rotate function can rotate multiple plots at once, and keep their relative positions unchanged. They can slso be rotated individually.
t = linspace(0, 1, 500);
r = 1;
x = r*cos(2*pi*t) + [0; 0; 0];
y = r*sin(2*pi*t) + [0; 0; 0];
z = ones(size(t)) + [0; 1; 2];
figure
hold on
for k = 1:size(x,1)
hp3(k) = plot3(x(k,:), y(k,:), z(k,:)); % Three Separate 3D Plots
end
hold off
grid on
axis('equal')
view(60,30)
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Original')
figure
hold on
for k = 1:size(x,1)
hp3(k) = plot3(x(k,:), y(k,:), z(k,:)); % Three Separate 3D Plots
end
hold off
grid on
axis('equal')
view(60,30)
xlabel('X')
ylabel('Y')
zlabel('Z')
rotate(hp3, [1 2 0], 45) % Call 'rotate'
title('Rotated Original')
Make appropriate changes to work with your data.
Thanking @Karim again for solving the .mat file load problem using the online Run feature, so not posting this as a separate Answer.
.
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!