如何在matplotlib中隐藏曲面图后面的线?

2024-04-16 04:15:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Matplotlib通过球体表面上的颜色贴图来绘制数据。另外,我想添加一个三维线图。到目前为止,我得到的代码是:

import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np


NPoints_Phi         = 30
NPoints_Theta       = 30

radius              = 1
pi                  = np.pi
cos                 = np.cos
sin                 = np.sin

phi_array           = ((np.linspace(0, 1, NPoints_Phi))**1) * 2*pi
theta_array         = (np.linspace(0, 1, NPoints_Theta) **1) * pi


phi, theta          = np.meshgrid(phi_array, theta_array) 


x_coord             = radius*sin(theta)*cos(phi)
y_coord             = radius*sin(theta)*sin(phi)
z_coord             = radius*cos(theta)


#Make colormap the fourth dimension
color_dimension     = x_coord 
minn, maxx          = color_dimension.min(), color_dimension.max()
norm                = matplotlib.colors.Normalize(minn, maxx)
m                   = plt.cm.ScalarMappable(norm=norm, cmap='jet')
m.set_array([])
fcolors             = m.to_rgba(color_dimension)



theta2              = np.linspace(-np.pi,  0, 1000)
phi2                = np.linspace( 0 ,  5 * 2*np.pi , 1000)


x_coord_2           = radius * np.sin(theta2) * np.cos(phi2)
y_coord_2           = radius * np.sin(theta2) * np.sin(phi2)
z_coord_2           = radius * np.cos(theta2)

# plot
fig                 = plt.figure()

ax                  = fig.gca(projection='3d')
ax.plot(x_coord_2, y_coord_2, z_coord_2,'k|-', linewidth=1 )
ax.plot_surface(x_coord,y_coord,z_coord, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
fig.show()

{生成一个图像,它看起来像是这个。但是,当黑线在背景中时,它应该被曲面图遮住,当它在前景中时,它应该是可见的。换句话说,黑线不应该“穿过”球体。在

可以在Matplotlib中不使用Mayavi来完成吗?在


Tags: importnppisincosarraycolorphi
1条回答
网友
1楼 · 发布于 2024-04-16 04:15:53

问题是matplotlib不是光线跟踪器,它并不是真正设计成一个支持三维打印的库。因此,它与二维空间中的层系统一起工作,对象可以位于层的前面或后面。这可以用zorder关键字参数对大多数绘图函数进行设置。然而,matplotlib中并没有意识到一个物体是在三维空间中另一个物体的前面还是后面。因此,您可以使完整的线可见(在球体前面)或隐藏(在球体后面)。在

解决方法是计算你自己应该看到的点。我在这里说点,因为一条线会“穿过”球体连接可见点,这是不需要的。因此,我限制自己去画点-但是如果你有足够多的点,它们看起来像一条线:-)。或者,可以通过在不需要连接的点之间使用额外的nan坐标来隐藏线;我将自己限制在这里的点,而不是使解决方案比需要的更复杂。在

对于一个完美的球体来说,计算哪些点应该是可见的并不困难,其思想如下:

  1. 获取三维打印的视角
  2. 在此基础上,计算出视场方向上数据坐标系下视场的法向量。在
  3. 计算这个法向量(在下面的代码中称为X)和线点之间的标量积,以便使用这个标量积作为是否显示点的条件。如果标量积小于0,则相应点位于观察平面的另一侧,因此不应显示。在
  4. 按条件过滤点。在

另一个可选任务是在用户旋转视图时调整显示的点。这是通过将motion_notify_event连接到一个函数来完成的,该函数使用上面的过程根据新设置的视角更新数据。在

请参阅下面的代码以了解如何实现这一点。在

import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np


NPoints_Phi         = 30
NPoints_Theta       = 30

phi_array           = ((np.linspace(0, 1, NPoints_Phi))**1) * 2*np.pi
theta_array         = (np.linspace(0, 1, NPoints_Theta) **1) * np.pi

radius=1
phi, theta          = np.meshgrid(phi_array, theta_array) 

x_coord             = radius*np.sin(theta)*np.cos(phi)
y_coord             = radius*np.sin(theta)*np.sin(phi)
z_coord             = radius*np.cos(theta)

#Make colormap the fourth dimension
color_dimension     = x_coord 
minn, maxx          = color_dimension.min(), color_dimension.max()
norm                = matplotlib.colors.Normalize(minn, maxx)
m                   = plt.cm.ScalarMappable(norm=norm, cmap='jet')
m.set_array([])
fcolors             = m.to_rgba(color_dimension)

theta2              = np.linspace(-np.pi,  0, 1000)
phi2                = np.linspace( 0, 5 * 2*np.pi , 1000)

x_coord_2           = radius * np.sin(theta2) * np.cos(phi2)
y_coord_2           = radius * np.sin(theta2) * np.sin(phi2)
z_coord_2           = radius * np.cos(theta2)

# plot
fig = plt.figure()

ax = fig.gca(projection='3d')
# plot empty plot, with points (without a line)
points, = ax.plot([],[],[],'k.', markersize=5, alpha=0.9)
#set initial viewing angles
azimuth, elev = 75, 21
ax.view_init(elev, azimuth )

def plot_visible(azimuth, elev):
    #transform viewing angle to normal vector in data coordinates
    a = azimuth*np.pi/180. -np.pi
    e = elev*np.pi/180. - np.pi/2.
    X = [ np.sin(e) * np.cos(a),np.sin(e) * np.sin(a),np.cos(e)]  
    # concatenate coordinates
    Z = np.c_[x_coord_2, y_coord_2, z_coord_2]
    # calculate dot product 
    # the points where this is positive are to be shown
    cond = (np.dot(Z,X) >= 0)
    # filter points by the above condition
    x_c = x_coord_2[cond]
    y_c = y_coord_2[cond]
    z_c = z_coord_2[cond]
    # set the new data points
    points.set_data(x_c, y_c)
    points.set_3d_properties(z_c, zdir="z")
    fig.canvas.draw_idle()

plot_visible(azimuth, elev)
ax.plot_surface(x_coord,y_coord,z_coord, rstride=1, cstride=1, 
            facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)

# in order to always show the correct points on the sphere, 
# the points to be shown must be recalculated one the viewing angle changes
# when the user rotates the plot
def rotate(event):
    if event.inaxes == ax:
        plot_visible(ax.azim, ax.elev)

c1 = fig.canvas.mpl_connect('motion_notify_event', rotate)

plt.show()

enter image description here

最后,为了得到最具视觉吸引力的结果,人们可能不得不对markersizealpha和点数进行一点调整。在

相关问题 更多 >