已经有一个帖子询问如何在matplotlib中绘制二维坐标系中的非正交轴线。
Draw non-orthogonal axis in matplotlib?
我想知道如何在三维情况下绘制这样的轴线。比如,当x和y轴不正交时,z轴垂直于x-y平面。例如,如何在曲线坐标系中为(1,2,0)和(3,2,0)绘制散点图呢?不限于使用matplotlib。谢谢!
这里有一个Matlab的例子。它适用于非正交轴,但不适用于曲线坐标系的完全一般情况。您可以使用矩阵A
来控制轴。它当前设置为使x轴与y轴成45度角。A = eye(3)
会使它们再次正交。
该脚本打开两个图。第一个是普通的正交三维图。第二个尊重坐标变换(非正交轴)。要检查它,请从上面看第二个图,您应该看到圆变成了椭圆。
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)