绘制3D非正交坐标

时间:2017-02-20 08:34:31

标签: matlab matplotlib plot gnuplot wolfram-mathematica

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

1 个答案:

答案 0 :(得分: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)

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