Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法用matplotlib绘制三维笛卡尔坐标系?_Python_Matplotlib_Plot - Fatal编程技术网

Python 有没有办法用matplotlib绘制三维笛卡尔坐标系?

Python 有没有办法用matplotlib绘制三维笛卡尔坐标系?,python,matplotlib,plot,Python,Matplotlib,Plot,我试图用matplotlib绘制一个3d,居中原点,用箭头指向3个方向,诸如此类 我已经用这个代码绘制了一个2d版本,基于这个 似乎ax.arrow不支持3d来实现这一点,因此,我必须使用quiver来绘制一个简单的3d版本 ax.quiver(0, 0, 0, 0, 3, 0, arrow_length_ratio=0.1) ax.quiver(0, 0, 0, 3, 0, 0, arrow_length_ratio=0.1) ax.quiver(0, 0, 0, 0, 0, 3,

我试图用matplotlib绘制一个3d,居中原点,用箭头指向3个方向,诸如此类

我已经用这个代码绘制了一个2d版本,基于这个

似乎
ax.arrow
不支持3d来实现这一点,因此,我必须使用quiver来绘制一个简单的3d版本

ax.quiver(0, 0, 0, 0, 3, 0, 
 arrow_length_ratio=0.1)
ax.quiver(0, 0, 0, 3, 0, 0, 
 arrow_length_ratio=0.1)
ax.quiver(0, 0, 0, 0, 0, 3, 
 arrow_length_ratio=0.1)
limt = 2
ax.set_xlim([-limt, limt])
ax.set_ylim([-limt, limt])
ax.set_zlim([-limt, limt])
还有这个

我不熟悉quiver,所以我不确定用matplotlib绘制三维笛卡尔坐标系是否可行


任何提示都将不胜感激。

我找到了两个有用的链接并将它们放在一起。也许这就是你想要的: 对于箭头: 对于三维立方体: 首先查看输出:


希望这有帮助。我还需要漂亮的箭头,所以如果你发现更好的,请发帖子;)

三维打印已经是Matplotlib的一部分!看见
ax.quiver(0, 0, 0, 0, 3, 0, 
 arrow_length_ratio=0.1)
ax.quiver(0, 0, 0, 3, 0, 0, 
 arrow_length_ratio=0.1)
ax.quiver(0, 0, 0, 0, 0, 3, 
 arrow_length_ratio=0.1)
limt = 2
ax.set_xlim([-limt, limt])
ax.set_ylim([-limt, limt])
ax.set_zlim([-limt, limt])
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
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)


def cuboid_data(center, size):
    # suppose axis direction: x: to left; y: to inside; z: to upper
    # get the (left, outside, bottom) point
    o = [a - b / 2 for a, b in zip(center, size)]
    # get the length, width, and height
    l, w, h = size
    x = np.array([[o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in bottom surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in upper surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in outside surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]]])  # x coordinate of points in inside surface
    y = np.array([[o[1], o[1], o[1] + w, o[1] + w, o[1]],  # y coordinate of points in bottom surface
         [o[1], o[1], o[1] + w, o[1] + w, o[1]],  # y coordinate of points in upper surface
         [o[1], o[1], o[1], o[1], o[1]],          # y coordinate of points in outside surface
         [o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]])    # y coordinate of points in inside surface
    z = np.array([[o[2], o[2], o[2], o[2], o[2]],                        # z coordinate of points in bottom surface
         [o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h],    # z coordinate of points in upper surface
         [o[2], o[2], o[2] + h, o[2] + h, o[2]],                # z coordinate of points in outside surface
         [o[2], o[2], o[2] + h, o[2] + h, o[2]]])                # z coordinate of points in inside surface
    return x, y, z


if __name__ == '__main__':
    center = [0, 0, 0]
    length = 1
    width = 1
    height = 1
    fig = plt.figure()
    ax1 = fig.add_subplot(111, projection='3d')
    X, Y, Z = cuboid_data(center, (length, width, height))
    ax1.plot_surface(X, Y, Z, color='b', rstride=1, cstride=1, alpha=0.1)
    ax1.set_xlabel('X')
    ax1.set_xlim(-1, 1)
    ax1.set_ylabel('Y')
    ax1.set_ylim(-1, 1)
    ax1.set_zlabel('Z')
    ax1.set_zlim(-1, 1)

    # Here we create the arrows:
    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')
    ax1.add_artist(a)
    a = Arrow3D([0, 0], [0, 1], [0, 0], **arrow_prop_dict, color='b')
    ax1.add_artist(a)
    a = Arrow3D([0, 0], [0, 0], [0, 1], **arrow_prop_dict, color='g')
    ax1.add_artist(a)

    # Give them a name:
    ax1.text(0.0, 0.0, -0.1, r'$0$')
    ax1.text(1.1, 0, 0, r'$x$')
    ax1.text(0, 1.1, 0, r'$y$')
    ax1.text(0, 0, 1.1, r'$z$')

    plt.show()