Drawing a righthand coordinate system in mplot3d

无人久伴 提交于 2019-12-22 22:02:34

问题


I want to create plots from Python of 3D coordinate transformations. For example, I want to create the following image (generated statically by TikZ):

A bit of searching led me to the following program:

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()

The result doesn't look like what one normally sees in books:

Furthermore, when I include the axes, the origin is not at the intersection of the three planes, which is where I expect it to be.

来源:https://stackoverflow.com/questions/47617952/drawing-a-righthand-coordinate-system-in-mplot3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!