在mplot3d中绘制一个右手坐标系

6
我想通过Python创建三维坐标转换的图表。例如,我想创建以下图像(由TikZ静态生成):

enter image description here

我找到了以下程序:

一点搜索让我找到了下面的程序:

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d


class Arrow3D(FancyArrowPatch):
    def __init__(self, xs, ys, zs, *args, **kwargs):
        FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
        self._verts3d = xs, ys, zs

    def draw(self, renderer):
        xs3d, ys3d, zs3d = self._verts3d
        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
        self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
        FancyArrowPatch.draw(self, renderer)


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
arrow_prop_dict = dict(mutation_scale=20, arrowstyle='->', shrinkA=0, shrinkB=0)

a = Arrow3D([0, 1], [0, 0], [0, 0], **arrow_prop_dict, color='r')
ax.add_artist(a)
a = Arrow3D([0, 0], [0, 1], [0, 0], **arrow_prop_dict, color='b')
ax.add_artist(a)
a = Arrow3D([0, 0], [0, 0], [0, 1], **arrow_prop_dict, color='g')
ax.add_artist(a)

ax.text(0.0, 0.0, -0.1, r'$o$')
ax.text(1.1, 0, 0, r'$x$')
ax.text(0, 1.1, 0, r'$y$')
ax.text(0, 0, 1.1, r'$z$')

ax.view_init(azim=-90, elev=90)
ax.set_axis_off()
plt.show()

结果看起来不像平常在书中看到的:

enter image description here

此外,当我包含轴时,原点并不在三个平面的交点处,这正是我所期望的位置。

谢谢@moti!我发现这个问题中的代码非常有用! - tacaswell
1
这个很好的例子在matplotlib>=3.5版本中出现了问题。在这个问题中有一个修复版本,链接在这里:https://github.com/matplotlib/matplotlib/issues/21688。 - john
1个回答

1
你可以通过更改以下代码行来改变3D坐标轴的视角:
ax.view_init(azim=-90, elev=90)

ax.view_init(azim=20, elev=10)

要复制您的TikZ图,或根据您的喜好更改视角的任何其他值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接