系列文章目錄
?文章來源地址http://www.zghlxwxcb.cn/news/detail-811761.html
前言
轉(zhuǎn)自:How to draw a 3D Christmas Tree with Matplotlib | by Timur Bakibayev, Ph.D. | Analytics Vidhya | Mediumhttps://medium.com/analytics-vidhya/how-to-draw-a-3d-christmas-tree-with-matplotlib-aabb9bc27864
因為我們把圣誕樹安裝在暖氣電池旁邊,所以它很快就死了。所以我決定用 Matplotlib 繪制一棵圣誕樹。你不需要為它遮陽避暑,它也不需要任何水。在阿瑞克斯星球,水的供應(yīng)是有限的。地球上也是如此。?
?文章來源:http://www.zghlxwxcb.cn/news/detail-811761.html
一、步驟
1.1?
要在 matplotlib 中繪圖,我們需要將其包含在內(nèi)。
此外,我們還要為 3D 準備所有庫。
import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
?讓我們先畫一個 3D 圓,確保一切正常。
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
k=300
Z = [10 for i in range(k)]
X = [math.cos(i/10) for i in range(k)]
Y = [math.sin(i/10) for i in range(k)]
ax.scatter(X,Y,Z, c="green", marker="^")
plt.show()
不錯!這是非常標準的。我們現(xiàn)在只修復(fù) Z 坐標。
現(xiàn)在,應(yīng)用 Z 坐標使其 3D 化。
Z = [i for i in range(k)]
?
讓我們在頂部縮小圓的半徑。
Z = [i for i in range(k)]
X = [math.cos(i/5)*(k-i) for i in range(k)]
Y = [math.sin(i/5)*(k-i) for i in range(k)]
?Matplotlib 總是傾向于貼合圖形,只需在此處添加這些限制即可:
?
plt.xlim(-500,500)
plt.ylim(-500,500)
?
?畫一些紅圈。它們的公式相同,但步長更大。我們還通過在 sin 和 cos 參數(shù)上加 2 來移動它,這樣它們就不會與樹本身相交。
k=300
Z = [i for i in range(k)]
X = [math.cos(i/5)*(k-i) for i in range(k)]
Y = [math.sin(i/5)*(k-i) for i in range(k)]
ax.scatter(X,Y,Z, c="green", marker="^")
k=300
step = 4
Z = [i for i in range(1,k,step)]
X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
ax.scatter(X,Y,Z, c="red", marker="o")
plt.xlim(-500,500)
plt.ylim(-500,500)
plt.show()
?微調(diào)裝飾
c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
ax.scatter(X,Y,Z, c=c, marker="o",s=40)
要旋轉(zhuǎn)樹形圖,我們需要為每一幀繪制樹形圖,并在 sin 和 cos 參數(shù)中添加一些常數(shù)。
我們?yōu)槌跏紙D形和每一幀復(fù)制代碼。
import math
import matplotlib.pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection="3d")
def init():
k=300
Z = [i for i in range(k)]
X = [math.cos(i/5)*(k-i) for i in range(k)]
Y = [math.sin(i/5)*(k-i) for i in range(k)]
ax.scatter(X,Y,Z, c="green", marker="^")
step = 3
c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
Z = [i for i in range(1,k,step)]
X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
ax.scatter(X,Y,Z, c=c, marker="o",s=40)
plt.xlim(-500,500)
plt.ylim(-500,500)
return fig,
def animate(f):
fig.clear()
ax = fig.add_subplot(111, projection="3d")
k=300
Z = [i for i in range(k)]
X = [math.cos(i/5+f/10)*(k-i) for i in range(k)]
Y = [math.sin(i/5+f/10)*(k-i) for i in range(k)]
ax.scatter(X,Y,Z, c="green", marker="^")
step = 3
c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
Z = [i for i in range(1,k,step)]
X = [math.cos(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
Y = [math.sin(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
ax.scatter(X,Y,Z, c=c, marker="o",s=40)
plt.xlim(-500,500)
plt.ylim(-500,500)
return fig,
ani=animation.FuncAnimation(fig, animate, init_func=init,
frames=90, interval=50, blit=True)
ani.save("christmas_tree.mp4")
?這就是結(jié)果:
?
不要忘記與您的朋友分享這棵樹!
新年快樂
點擊此處查看我的其他文章:timurbakibayev.medium.com
timurbakibayev.medium.comhttp://timurbakibayev.medium.com/
?
?
?
到了這里,關(guān)于Python - Matplotlib 繪制 3D 圣誕樹的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!