微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 matplotlib 的 3d 表面中到达固体表面?

如何解决如何在 matplotlib 的 3d 表面中到达固体表面?

请问如何在 matplotlib 的 3d 曲面中达到实体曲面?我尝试多次应用 plot_surface,但改进有限且不够。透明度还是很明显的。更多的样本也没有帮助。

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

# Coordinate for the cylinder
def data_for_cylinder_along_z(center_x,center_y,radius,height_z):
    z = np.linspace(0,height_z,200)
    theta = np.linspace(0,2*np.pi,200)
    theta_grid,z_grid=np.meshgrid(theta,z)
    x_grid = radius*np.cos(theta_grid) + center_x
    y_grid = radius*np.sin(theta_grid) + center_y
    return x_grid,y_grid,z_grid

fig = plt.figure(figsize = (5,10))
ax = fig.add_subplot(111,projection='3d')

ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_zlim(0,10)
 
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax),np.diag([0.4,0.4,1.6,1])) # (x,y,z) ration of sides

theta1 = np.linspace(0,100)
r1 = np.linspace(-2,100)
t1,R1 = np.meshgrid(theta1,r1)

X1 = R1*np.cos(t1)
Y1 = R1*np.sin(t1)
Z1 = 3+R1*1.5+4

ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')

ax.plot_surface(X1,Y1,Z1,color="dimgray")
ax.plot_surface(X1,color="dimgray")

# Cylinder
Xc,Yc,Zc = data_for_cylinder_along_z(0,2,4)

rep=10
for i in range(rep):
    ax.plot_surface(Xc,Zc,color = 'palegoldenrod')


plt.show()

解决方法

您需要将 antialiased 设置为 True(通过反复试验发现,在文档中找不到说明):

# Cylinder
Xc,Yc,Zc = data_for_cylinder_along_z(0,2,4)
ax.plot_surface(Xc,Zc,color='palegoldenrod',antialiased=False)

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。