简体   繁体   English

如何绘制带有网格的圆柱体?

[英]How to plot a cylinder with grid on it?

I would like to plot a cylinder centered on (0,0,0) given its height (h = 40) and radius (r = 20). 我想绘制一个以(0,0,0)为中心的圆柱体,给出其高度(h = 40)和半径(r = 20)。 I also want to create a grid on its surface. 我还想在其表面上创建一个网格。

My problem is the following: when I try to plot the top and bottom parts of my cylinder, they appear as squares on my figure and not as disks. 我的问题如下:当我尝试绘制圆柱体的顶部和底部时,它们在我的图形上显示为正方形而不是磁盘。 Also, when I set the grid for the lateral part using the wireframe function from Axes3D (matplotlib), for some reason, a grid appears on the top part and the squares don't have a constant size. 另外,当我使用Axes3D(matplotlib)中的线框功能为外侧部分设置网格时,由于某种原因,网格会出现在顶部,并且正方形的大小并不恒定。

fig1 = plt.figure(figsize = (10,10)) #On nomme la figure
env = fig1.add_subplot(111, projection='3d') #On crée le volume 3D avec quadrillage et axes


# Parois latérales

no_values = 1000
phi_cyl = np.linspace(0, 2*np.pi, no_values)
x_walls = R * np.cos(phi_cyl) #Valeurs de X prises par le cylindre externe
z_walls = np.linspace(-h/2, h/2, no_values)
x2D_walls, z2D_walls = np.meshgrid(x_walls, z_walls)
y2D_walls = np.sqrt(R**2 - x2D_walls**2)
env.plot_surface(x2D_walls, y2D_walls, z2D_walls, color='k', alpha=0.03)
env.plot_surface(x2D_walls, -y2D_walls, z2D_walls, color='k', alpha=0.03,)
env.plot_wireframe(x2D_walls, y2D_walls, z2D_walls, rstride=2, cstride =2)
env.plot_wireframe(x2D_walls, -y2D_walls, z2D_walls, rstride=2, cstride =2)


#Plafond

phi_cyl = np.linspace(0, 2*np.pi, no_values)
x_ceiling = R * np.cos(phi_cyl)
y_ceiling = R * np.sin(phi_cyl)
x2D_ceiling, y2D_ceiling = np.meshgrid(x_ceiling, y_ceiling)
z2D_ceiling = np.ones((len(x_ceiling),len(y_ceiling)))* h/2
env.plot_surface(x2D_ceiling, y2D_ceiling, z2D_ceiling, color='k', alpha=0.03)
env.plot_wireframe(x2D_ceiling, y2D_ceiling, z2D_ceiling, rstride=2, cstride =2)


#Sol

phi_cyl = np.linspace(0, 2*np.pi, no_values)
x_floor = R * np.cos(phi_cyl)
y_floor = R * np.sin(phi_cyl)
x2D_floor, y2D_floor = np.meshgrid(x_floor, y_floor)
z2D_floor = - np.ones((len(x_floor),len(y_floor)))* h/2
env.plot_surface(x2D_floor, y2D_floor, z2D_floor, color='k', alpha=0.03)
env.plot_surface(x2D_floor, y2D_floor, z2D_floor, color='k', alpha=0.03, rstride=2, cstride=2)



plt.title('Détecteur T2K')
env.set_xlabel('Profondeur [m]')
env.set_ylabel('Largeur [m]')
env.set_zlabel('Hauteur [m]')
plt.show()
plt.savefig("Cuve")

I would like to get a grid with squares of a size 0.5*0.5 and to finally close my cylinder. 我想要一个尺寸为0.5 * 0.5的正方形的网格,最后关闭圆柱体。 Thank you for your help 谢谢您的帮助

So mesh grid is what is causing you to have a rectangular grid at the top and bottom of your cylinder because both x_ceiling and y_ceiling will be defined at -20, and positive 20. You can use the np.outer to draw circles at the top and bottom boundary, but this puts the circle in a type of polar coordinates that does not give you .5x.5 boxes. 因此,网格是导致圆柱体顶部和底部具有矩形网格的原因,因为x_ceiling和y_ceiling都将定义为-20,且正数为20。您可以使用np.outer在顶部绘制圆和底部边界,但这会使圆处于一种极坐标类型,不会给您0.5 x.5的框。 I haven't been able to find another workaround so maybe someone else will have a better answer but this could be a temporary solution. 我无法找到其他解决方法,因此也许其他人会有更好的答案,但这可能是一个临时解决方案。

from mpl_toolkits.mplot3d import Axes3D
fig1 = plt.figure(figsize = (10,10)) #On nomme la figure
env = fig1.add_subplot(111, projection='3d') #On crée le volume 3D avec quadrillage et axes

R=20.
h=40.
# Parois latérales

no_values = 100.
phi_cyl = np.linspace(0, 2*np.pi, no_values)
x_walls = R * np.cos(phi_cyl) #Valeurs de X prises par le cylindre externe
z_walls = np.linspace(-h/2, h/2, no_values)
x2D_walls, z2D_walls = np.meshgrid(x_walls, z_walls)
y2D_walls = np.sqrt(R**2 - x2D_walls**2)


env.plot_surface(x2D_walls, y2D_walls, z2D_walls, color='k', alpha=0.03)
env.plot_surface(x2D_walls, -y2D_walls, z2D_walls, color='k', alpha=0.03,)
env.plot_wireframe(x2D_walls, y2D_walls, z2D_walls, rstride=2, cstride =2)
env.plot_wireframe(x2D_walls, -y2D_walls, z2D_walls, rstride=2, cstride =2)


#Plafond

phi_cyl = np.linspace(0, 2*np.pi, no_values)

#Set the radii intervals for the circle
Rs = np.arange(0,R+1,1)

x_ceiling = np.outer(Rs, np.cos(phi_cyl))
y_ceiling = np.outer(Rs, np.sin(phi_cyl))

z2D_ceiling = np.ones((y_ceiling.shape))* h/2.
env.plot_surface(x_ceiling, y_ceiling, z2D_ceiling, color='k', alpha=0.03)
env.plot_wireframe(x_ceiling, y_ceiling, z2D_ceiling, rstride=2, cstride =2)


#Sol

phi_cyl = np.linspace(0, 2*np.pi, no_values)
Rs = np.arange(0,R+1,1)
x_floor = np.outer(Rs, np.cos(phi_cyl))
y_floor = np.outer(Rs, np.sin(phi_cyl))

z2D_floor = np.ones((y_floor.shape))* -h/2.
env.plot_surface(x_floor, y_floor, z2D_floor, color='k', alpha=0.03)
env.plot_wireframe(x_floor, y_floor, z2D_floor, rstride=2, cstride =2)



plt.show()

This would yield the following figure: 这将产生下图:

在此处输入图片说明

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

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