簡體   English   中英

繪制圓錐體時斷開的曲面

[英]Disconnected surfaces when plotting cones

我想用python繪制曲面 (z+1)²=x²+y² 和 4z=x²+y² 。

我寫了這段代碼:

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

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
X= np.arange(-2,3,.1)
Z=np.arange(0,2,.1)
X,Z = np.meshgrid(X,Z)
Y=np.sqrt((Z+1)**2-X**2)
Y2=np.sqrt(4*Z-X**2)
ax.plot_wireframe(X, Y, Z, rstride = 1, cstride =1)
ax.plot_wireframe(X, -Y, Z, rstride = 1, cstride =1)
ax.plot_surface(X,Y2,Z,rstride=1,cstride=1,color='red')
ax.plot_surface(X,-Y2,Z,rstride=1,cstride=1,color='red')
ax.set_zlim(0,2)

plt.show()

這將必須顯示兩個錐體。 但是,每個錐體都不是連續的,即缺少一些面,我不知道為什么。 任何幫助將不勝感激。

您定義 X 和 Y 的方式在這些連接處引起了一些驚愕。 您可以通過在將圓錐體轉換為 X 和 Y 之前根據半徑和角度定義圓錐體來獲得更平滑的連接,這樣您就可以保持以舊方式生成的漂亮 Z 輪廓。

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

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

# Set up the grid in polar
theta = np.linspace(0,2*np.pi,90)
r = np.linspace(0,3,50)
T, R = np.meshgrid(theta, r)

# Then calculate X, Y, and Z
X = R * np.cos(T)
Y = R * np.sin(T)
Z = np.sqrt(X**2 + Y**2) - 1

# Set the Z values outside your range to NaNs so they aren't plotted
Z[Z < 0] = np.nan
Z[Z > 2.1] = np.nan
ax.plot_wireframe(X, Y, Z)

ax.set_zlim(0,2)

plt.show()

這會給你一個非常好的錐體:圓錐體的線框圖

您的曲面已損壞,因為您正在為每個錐體繪制兩個單獨的曲面。 使每個錐體成為完整、連續曲面的一種方法是制作一個 x 和 y 網格,然后為每個錐體僅繪制一個曲面:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xvec = np.arange(-2, 3, 0.1)
yvec = np.arange(-3, 3, 0.1)
X, Y = np.meshgrid(xvec, yvec)
Z1 = np.sqrt(X**2 + Y**2) - 1 
Z2 = (X**2 + Y**2)/4.
ax.plot_wireframe(X, Y, Z1, rstride=1, cstride=1)
ax.plot_surface(X, Y, Z2, rstride=1, cstride=1, color='red')
ax.set_zlim(0,2)
plt.show()

這太神秘了......一般情況是

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')


#xyz position of tip of cone, radius of the end of the cone, and height of the cone
radi = 4
height = 2
a=1 #x
b=1 #y
c=0 #z
choose=max(radi,height)


# Set up the grid in polar
theta = np.linspace(0,2*np.pi,90)
r = np.linspace(0,choose,50)
T, R = np.meshgrid(theta, r)

# Then calculate X, Y, and Z
X = R * np.cos(T) + a
Y = R * np.sin(T) + b
Z = (np.sqrt((X-a)**2 + (Y-b)**2)/(radi/height)) + c

ax.plot_wireframe(X, Y, Z)

ax.set_zlim(-1.2,2.2)

plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM