The 3D plots generated by Matplotlib are rendered from a perspective projection. Due to this, the Z axis is not vertical when rendered from certain rotation angles or elevations. Matplotlib was designed inspired from the Matlab plotting API. In contrast, in Matlab it is pretty easy to get orthographic projection in 3D plots where the Z axis is perfectly vertical.
Orthographic projection is currently not supported in Matplotlib. A pull request with code has been given for it as seen here. We can adapt that code in our Python scripts to get a vertical Z axis. Just use this code snippet:
import numpy from mpl_toolkits.mplot3d import proj3d def orthogonal_proj(zfront, zback): a = (zfront+zback)/(zfront-zback) b = -2*(zfront*zback)/(zfront-zback) # -0.0001 added for numerical stability as suggested in: # http://stackoverflow.com/questions/23840756 return numpy.array([[1,0,0,0], [0,1,0,0], [0,0,a,b], [0,0,-0.0001,zback]]) # Later in your plotting code ... proj3d.persp_transformation = orthogonal_proj
As you can notice, we add
-0.0001 as a small perturbation from
0 to give the code numerical stability, as suggested here. Without this, the Z axis could be generated as inverted or at the wrong end of the plot. By adding the above code, I was able to get orthographic project for my 3D plots with perfectly vertical Z axis.
Tried with: Matplotlib 1.3.1, Python 2.7.6 and Ubuntu 14.04