绘制3D非正交坐标

qua*_*hou 9 matlab plot wolfram-mathematica gnuplot matplotlib

已经有一篇文章询问如何在matplotlib中的2D坐标系中绘制非正交轴. 在matplotlib中绘制非正交轴? 我想知道如何在3D情况下绘制这样的轴?假设z轴垂直于xy平面,而x轴和y轴不垂直.例如,如何在曲线系统中绘制(1,2,0)和(3,2,0)的散射三维图?不限于使用matplotlib.谢谢!

小智 0

这是 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)
Run Code Online (Sandbox Code Playgroud)

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