Reputation: 610
I'm plotting 3d lines and points in matplotlib. On the output, there is always on top the last data plotted and not the "closer" data in the 3d projection. I would like to achieve a view that considers the perspective.
Here's a minimal code, In this case the red line is always on top of the blue line:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize = [8, 14])
ax1 = fig.add_subplot(211, projection='3d')
ax2 = fig.add_subplot(212, projection='3d')
x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
ax1.plot(x, y, 0, 'b', lw = 3)
ax1.plot(x, y, 1, 'r', lw = 3)
ax2.plot(x, y, 0, 'b', lw = 3)
ax2.plot(x, y, 1, 'r', lw = 3)
ax1.view_init(90, 0)
ax2.view_init(-90, 0)
plt.show()
Upvotes: 3
Views: 9482
Reputation: 161
As of matplotlib 2.1.0, you can set the projection type to orthographic by calling ax.set_proj_type('ortho')
. So for your example:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize = [8, 14])
ax1 = fig.add_subplot(211, projection='3d')
ax2 = fig.add_subplot(212, projection='3d')
ax1.set_proj_type('ortho')
ax2.set_proj_type('ortho')
x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
ax1.plot(x, y, 0, 'b', lw = 3)
ax1.plot(x, y, 1, 'r', lw = 3)
ax2.plot(x, y, 0, 'b', lw = 3)
ax2.plot(x, y, 1, 'r', lw = 3)
ax1.view_init(90, 0)
ax2.view_init(-90, 0)
plt.show()
Upvotes: 3