Is it possible to disable the perspective when plotting in mplot3d, i.e. to use the orthogonal projection?
Asked
Active
Viewed 1.3k times
2 Answers
35
This is now official included since matplot version 2.2.2 Whats new | github
So for plotting a perspective orthogonal plot you have to add proj_type = 'ortho'
then you should have something like that:
fig.add_subplot(121, projection='3d', proj_type = 'ortho')
Example Picture
]2
Example is taken from the official example script and edited
'''
======================
3D surface (color map)
======================
Demonstrates plotting a 3D surface colored with the coolwarm color map.
The surface is made opaque by using antialiased=False.
Also demonstrates using the LinearLocator and custom formatting for the
z axis tick labels.
'''
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot the surface.
fig = plt.figure(figsize=(16,4))
ax.view_init(40, 60)
ax = fig.add_subplot(121, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
ax = fig.add_subplot(122, projection='3d', proj_type = 'ortho')
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.viridis, linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
plt.show()

TimK
- 615
- 1
- 7
- 12
-
4This answer is the one that currently works, thanks!! – UselesssCat Sep 09 '18 at 01:52
12
NOTE: This has been updated see this answer instead.
Sort of, you can run this snippet of code before you plot:
import numpy
from mpl_toolkits.mplot3d import proj3d
def orthogonal_proj(zfront, zback):
a = (zfront+zback)/(zfront-zback)
b = -2*(zfront*zback)/(zfront-zback)
return numpy.array([[1,0,0,0],
[0,1,0,0],
[0,0,a,b],
[0,0,0,zback]])
proj3d.persp_transformation = orthogonal_proj
It is currently an open issue found here.

Dair
- 15,910
- 9
- 62
- 107
-
It worked, thanks! The axes directions look opposite though, but this could probably be solved playing with the signs/order in this transformation matrix. – user3670781 May 26 '14 at 00:01
-
2It seems, that putting a small negative number (e.g. -0.0001) instead of zero in the third column of the last row helps avoiding matrix singularity problems, and solves a strange inversion of axes direction. – user3670781 May 26 '14 at 00:16
-
-
1
-
1@AhmadSultan: I would guess you need to store the original projection before replacing it, something like `persp_proj = proj3d.persp_transformation` before assigning `orthogonal_proj` to it. But starting version 2.2.2 you can just set_proj_type('persp'). – danuker Jul 20 '19 at 14:49
-
-
1