gpt4 book ai didi

matplotlib - 绘制3D非正交坐标

转载 作者:太空宇宙 更新时间:2023-11-03 19:20:58 30 4
gpt4 key购买 nike

已经有帖子问如何在matplotlib中绘制二维坐标系中的非正交轴。 Draw non-orthogonal axis in matplotlib?我想知道如何在 3D 案例中绘制这样的轴?假设 z 轴垂直于 x-y 平面,而 x 和 y 轴不正交。例如,如何在曲线系统中绘制 (1,2,0) 和 (3,2,0) 的散点 3D 图?不限制使用 matplotlib。谢谢!

最佳答案

这是 Matlab 中的示例。它适用于非正交轴,但不适用于曲线坐标的完整一般情况。您可以使用矩阵 A 控制轴。当前设置为使 x 轴与 y 轴成 45 度角。 A = eye(3) 将使它们再次正交。

脚本打开两个图。第一个是正常的正交 3d 图。第二个是坐标变换(非正交轴)。要检查它,请查看上面的第二张图,您应该会看到圆圈变成了椭圆形。

close all
clear

fx = @(t) t.^2 .* cos(2*t);
fy = @(t) t.^2 .* sin(2*t);
fz = @(t) t;
t = linspace(0,6*pi,400);

figure
plot3(fx(t), fy(t), fz(t));
grid on

xTicks = get(gca, 'XTick');
yTicks = get(gca, 'YTick');
zTicks = get(gca, 'ZTick');

% coordinate transform: x = Aq
A = [sqrt(2)/2 sqrt(2)/2 0
0 1 0
0 0 1];
%A = eye(3);

figure
hold on

% draw the function
Q = [fx(t); fy(t); fz(t)];
X = A*Q;
plot3(X(1,:), X(2,:), X(3,:))

% draw x grid lines
x = [xTicks
xTicks
xTicks];
y = repmat([min(yTicks); max(yTicks); max(yTicks) ], 1, length(xTicks));
z = repmat([min(zTicks); min(zTicks); max(zTicks) ], 1, length(xTicks));
X = A*[x(:)'; y(:)'; z(:)'];
line(reshape(X(1,:), 3, []),...
reshape(X(2,:), 3, []),...
reshape(X(3,:), 3, []), 'color', [.8 .8 .8]);

% draw y grid lines
y = [yTicks
yTicks
yTicks];
x = repmat([min(xTicks); max(xTicks); max(xTicks) ], 1, length(yTicks));
z = repmat([min(zTicks); min(zTicks); max(zTicks) ], 1, length(yTicks));
X = A*[x(:)'; y(:)'; z(:)'];
line(reshape(X(1,:), 3, []),...
reshape(X(2,:), 3, []),...
reshape(X(3,:), 3, []), 'color', [.8 .8 .8]);

% draw z grid lines
z = [zTicks
zTicks
zTicks];
x = repmat([min(xTicks); max(xTicks); max(xTicks) ], 1, length(zTicks));
y = repmat([max(yTicks); max(yTicks); min(yTicks) ], 1, length(zTicks));
X = A*[x(:)'; y(:)'; z(:)'];
line(reshape(X(1,:), 3, []),...
reshape(X(2,:), 3, []),...
reshape(X(3,:), 3, []), 'color', [.8 .8 .8]);

% draw grid planes
q{1} = [xTicks(1) xTicks(1) xTicks(end) xTicks(end)
yTicks(1) yTicks(end) yTicks(end) yTicks(1)
zTicks(1) zTicks(1) zTicks(1) zTicks(1)];
q{2} = [xTicks(end) xTicks(end) xTicks(end) xTicks(end)
yTicks(1) yTicks(1) yTicks(end) yTicks(end)
zTicks(1) zTicks(end) zTicks(end) zTicks(1)];
q{3} = [xTicks(1) xTicks(1) xTicks(end) xTicks(end)
yTicks(end) yTicks(end) yTicks(end) yTicks(end)
zTicks(1) zTicks(end) zTicks(end) zTicks(1)];
for i = 1:3
x = A*q{i};
fill3(x(1,:), x(2,:), x(3,:), [1 1 1]);
end

% cleanup and set view
axis off
view(-35, 30)

基本思想是我们需要根据坐标变换手动绘制图中的每个元素(轴、网格等)。这很痛苦,但可以奏效。

关于matplotlib - 绘制3D非正交坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42339755/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com