简体   繁体   English

如何在Matplotlib 3D图中实现透视图

[英]How to achieve perspective in matplotlib 3d plot

I'm plotting 3d lines and points in matplotlib. 我在matplotlib中绘制3d线和点。 On the output, there is always on top the last data plotted and not the "closer" data in the 3d projection. 在输出上,总是在最后绘制的数据位于顶部,而不是3d投影中的“较近”数据。 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') . 从matplotlib 2.1.0开始,您可以通过调用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM