二次曲面
python中繪制三維圖需要將坐標系宣告為3d,
球面方程為
x 2 + y 2 + z 2 = R 2 x^2+y^2+z^2=R^2 x2+y2+z2=R2
寫為極坐標形式為
x = R sin ? θ cos ? φ y = R sin ? θ sin ? φ z = R cos ? θ \begin{aligned} x&=R\sin\theta\cos\varphi\\ y&=R\sin\theta\sin\varphi\\ z&=R\cos\theta\end{aligned} xyz?=Rsinθcosφ=Rsinθsinφ=Rcosθ?
令 R = 1 R=1 R=1,則畫圖為

代碼如下
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> theta = np.arange(0,6.4,0.1).reshape(64,1)
>>> phi = np.arange(0,3.2,0.1).reshape(1,32)
>>> x = np.sin(theta)*np.cos(phi)
>>> y = np.sin(theta)*np.sin(phi)
>>> z = np.cos(theta)
>>> ax = plt.gca(projection='3d')
>>> ax.plot_surface(x,y,z)
<mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x000001CECF13A730>
>>> plt.show()
二次曲面共有九種,代碼均與橢球曲面類似,為了加強立體感,可在畫圖的時候設定顏色映射,下列各圖部分用到
from matplotlib import cm
#...
ax.plot_surface(x,y,z,cmap=cm.coolwarm)
| a,b,c均為1時的曲面 | |
|---|---|
| 橢圓錐面 x 2 a 2 + y 2 b 2 ? z 2 c 2 = 0 \frac{x^2}{a^2}+\frac{y^2}{b^2}-\frac{z^2}{c^2}=0 a2x2?+b2y2??c2z2?=0 | ![]() |
| 橢球面 x 2 a 2 + y 2 b 2 + z 2 c 2 = 1 \frac{x^2}{a^2}+\frac{y^2}{b^2}+\frac{z^2}{c^2}=1 a2x2?+b2y2?+c2z2?=1 | ![]() |
| 單葉雙曲面 x 2 a 2 + y 2 b 2 ? z 2 c 2 = 1 \frac{x^2}{a^2}+\frac{y^2}{b^2}-\frac{z^2}{c^2}=1 a2x2?+b2y2??c2z2?=1 | ![]() |
| 雙葉雙曲面 x 2 a 2 + y 2 b 2 ? z 2 c 2 = ? 1 \frac{x^2}{a^2}+\frac{y^2}{b^2}-\frac{z^2}{c^2}=-1 a2x2?+b2y2??c2z2?=?1 | ![]() |
| 橢圓拋物面 z = x 2 a 2 + y 2 b 2 z=\frac{x^2}{a^2}+\frac{y^2}{b^2} z=a2x2?+b2y2? | ![]() |
| 雙曲拋物面 z = x 2 a 2 ? y 2 b 2 z=\frac{x^2}{a^2}-\frac{y^2}{b^2} z=a2x2??b2y2? | ![]() |
| 橢圓柱面 x 2 a 2 + y 2 b 2 = 1 \frac{x^2}{a^2}+\frac{y^2}{b^2}=1 a2x2?+b2y2?=1 | ![]() |
| 雙曲柱面 x 2 a 2 ? y 2 b 2 = 1 \frac{x^2}{a^2}-\frac{y^2}{b^2}=1 a2x2??b2y2?=1 | ![]() |
| 拋物柱面 y 2 = 2 p x y^2=2px y2=2px | ![]() |
在上面各式中,橢圓錐面、單葉雙曲面、雙葉雙曲面具有極為相似的運算式
x 2 a 2 + y 2 b 2 ? z 2 c 2 = { < 0 雙 葉 雙 曲 面 = 0 橢 圓 錐 面 > 0 單 葉 雙 曲 面 \frac{x^2}{a^2}+\frac{y^2}{b^2}-\frac{z^2}{c^2}=\left\{\begin{aligned} &<0&雙葉雙曲面\\ &=0&橢圓錐面\\ &>0&單葉雙曲面\\ \end{aligned}\right. a2x2?+b2y2??c2z2?=???????<0=0>0?雙葉雙曲面橢圓錐面單葉雙曲面?
故可繪制動態圖來表示這一程序,由于animation中無法繪制plot_surface,所以采用將單張圖片生成gif的方式,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import imageio
theta = np.arange(0,6.4,0.1)
z = np.arange(-2,2,0.02).reshape(200,1)
gifImgs = []
fig = plt.figure()
for i in np.arange(-1,1,0.02):
theta = np.arange(0,6.4,0.1).reshape(1,64)
Z = np.repeat(z,64).reshape(200,64)
x = np.sqrt(z**2+i)*np.cos(theta)
y = np.sqrt(z**2+i)*np.sin(theta)
ax = plt.gca(projection='3d')
ax.plot_surface(x,y,Z,cmap=cm.coolwarm)
plt.savefig("%.2f.jpg" % i)
gifImgs.append(imageio.imread("%.2f.jpg" % i))
imageio.mimsave("test.gif",gifImgs,fps=5)

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/308776.html
標籤:python
下一篇:Python中的運算子介紹(下)









