简体   繁体   中英

How to achieve perspective in matplotlib 3d plot

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

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

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