Python 2D circular surface in 3D

一世执手 提交于 2019-12-24 09:39:50

问题


I am trying to generate the top/bottom of a cylindrical surface. I was able to obtain the lateral surface here: Generating a Cylindrical Surface with np.outer. I would like to use np.outer again for consistency. I thought I understood the answers in the link however if I understood correctly then the following should work:

R = 5
h = 5
u = np.linspace(0,  2*np.pi, 100)
x = R * np.outer(np.ones(np.size(u)), np.cos(u))          
y = R * np.outer(np.ones(np.size(u)), np.sin(u))          
z = h * np.outer(np.ones(np.size(u)), np.ones(np.size(u)))

however in my plots, no surface is generated. Am I still not using np.outer correctly? Why is no surface generated?


回答1:


There is no visible disk because all the points which you are creating have exactly the same distance to the center and surface which spans between "inner circle" and "outer circle" is infinitely thin. In order to see the disk, radius needs to vary between 0 and your desired value (5 in the example).

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

R = np.linspace(0, 5, 100)
h = 5
u = np.linspace(0,  2*np.pi, 100)

x = np.outer(R, np.cos(u))
y = np.outer(R, np.sin(u))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x,y,h) # z in case of disk which is parallel to XY plane is constant and you can directly use h
fig.show()



来源:https://stackoverflow.com/questions/43925577/python-2d-circular-surface-in-3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!