简体   繁体   中英

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

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. 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. 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:

在此处输入图片说明

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