How can I set the orientation for a 3D Matplotlib Figure?


I am attempting to plot a 3D surface, with the following code:

fig = plt.figure()
ax = fig.add_subplot(projection = '3d')
ax.plot_surface(x, y, z)
ax.set_aspect('equal') # not relevent to the problem

Now, this outputs a figure, as one would expect, but the problem is that the axes are a little unconventional.enter image description here

I would like to output the figure such that the x, y (not necessarily z) axes zeros line up, or essentially, like the following (which can be obtained by just panning the figure manually):enter image description here

Is there a matplotlib.pyplot command I’m missing?

Asked By: Jacob Ivanov




Include the line ax.view_init(45,45) before calling


ax.view_init(...) is a command which sets the initial perspective of a subplot when is called. It takes in four parameters elev=None, azim=None, roll=None, vertical_axis='z'. All of them have a default, so for your purpose, you will only need to include the two float values for the elevation and azimuth.

Link to documentation:

Answered By: trent
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.