Python Matplotlib Axed3D轴是否可以设置为与数学教科书绘制它们的方式在零处相交?

Python Matplotlib Axed3D轴是否可以设置为与数学教科书绘制它们的方式在零处相交?,python,matplotlib,Python,Matplotlib,通常,在二维matplotlib绘图中,您会得到笛卡尔平面的第一个量纲,轴线位于图形的左侧和底部区域。因为我是为了数学笔记而绘图的,所以我想要四象限布局,它可以通过以下方式完成: x = np.arange(-10, 10, 0.1) y = np.cos(x) f = plt.figure() ax = f.add_subplot(1, 1, 1) ax.spines['left'].set_position('center') ax.spines['right'].set_color('n

通常,在二维matplotlib绘图中,您会得到笛卡尔平面的第一个量纲,轴线位于图形的左侧和底部区域。因为我是为了数学笔记而绘图的,所以我想要四象限布局,它可以通过以下方式完成:

x = np.arange(-10, 10, 0.1)
y = np.cos(x)

f = plt.figure()
ax = f.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')

ax.plot(x, y, c='black', linewidth=1) 
ax.fill_between(x, y, where=(x > -1e-10) & (x < np.pi), color='gray')

ax.set_xbound(-2*np.pi, 2*np.pi)
ax.set_ybound(-3, 3)

f.suptitle("Area under a cosine function", size='x-large', weight='bold')
f.show()
问题是现在的数据和数据似乎不一致;轴在每个方向上都被设置为(我认为)5到5,但是数据,从-5到5,看起来它超出了轴的“边界”


试试这段代码,把它放在ax.scatter()之后。:


这将替换代码中的所有
ax.add\u artist()

您可以使用
ax3d关闭轴。设置\u axis\u off()
,然后使用
ax3d.add\u artist(Arrow3D())
创建自己的轴线。哪个类是
Arrow3D
?我好像找不到它。
x = np.arange(-5, 5)
y = np.arange(-5, 5)
z = np.arange(-5, 5)

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)

f = plt.figure()
ax = f.add_subplot(1, 1, 1, projection='3d')
ax.set_axis_off()
ax.add_artist(Arrow3D([-5, 5], [0, 0], [0, 0]))
ax.add_artist(Arrow3D([0, 0], [-5, 5], [0, 0]))
ax.add_artist(Arrow3D([0, 0], [0, 0], [-5, 5]))

ax.scatter(x, y, z)

f.show()
ax.add_artist(Arrow3D([-5, 5], [0, 0], [0, 0], mutation_scale=20, lw=1, arrowstyle="-|>", color="r"))
ax.add_artist(Arrow3D([0, 0], [-5, 5], [0, 0], mutation_scale=20, lw=1, arrowstyle="-|>", color="b"))
ax.add_artist(Arrow3D([0, 0], [0, 0], [-5, 5], mutation_scale=20, lw=1, arrowstyle="-|>", color="g"))

# set viewing angle
ax.azim = 40    # z rotation (default=270)
ax.elev = 25    # x rotation (default=0)
ax.dist = 10    # zoom (define perspective)