简体   繁体   中英

Can Matplotlib Axed3D axes be set to all intersect at zero the way math textbooks draw them?

Normally, in a 2D matplotlib plot, you get the 1st quandrant of the cartesian plane, with the axis lines on the left and bottom areas of the graph. Because I am plotting for the purposes of math notes, I wanted the four-quadrant layout which can be done with this:

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

Image

Now I want to do it with 3D plots, where the x, y, z axis lines all intersect at the origin so that the "spines" (I know Axes3D objects don't actually have spines) separates the space into 8 quadrants. How can I achieve this?

Like this Not like this

EDIT : Current 3d plot code:

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

The problem is now the data and the data doesn't seem lined up; the axes are set to (I think) -5 to 5 in each direction but the data, being from -5 to 5 as well, looks like it's exceeding the "boundaries" of the axis?

Current Solution

Try this piece of code, putting it after ax.scatter() :

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)

This replaces all ax.add_artist() in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM