Python使用Matplotlib繪制3D圣誕樹
前言
因?yàn)槲覀儼咽フQ樹安裝在暖氣電池旁邊,所以它很快就死了。所以我決定用 Matplotlib 繪制一棵圣誕樹。你不需要為它遮陽避暑,它也不需要任何水。在阿瑞克斯星球,水的供應(yīng)是有限的。地球上也是如此。
步驟
要在 matplotlib 中繪圖,我們需要將其包含在內(nèi)。
此外,我們還要為 3D 準(zhǔn)備所有庫。
import math import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection="3d")
讓我們先畫一個(gè) 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()

不錯(cuò)!這是非常標(biāo)準(zhǔn)的。我們現(xiàn)在只修復(fù) Z 坐標(biāo)。
現(xiàn)在,應(yīng)用 Z 坐標(biāo)使其 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é)果:

到此這篇關(guān)于Python使用Matplotlib繪制3D圣誕樹的文章就介紹到這了,更多相關(guān)Python Matplotlib繪制3D圣誕樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python結(jié)合OpenCV實(shí)現(xiàn)簡單的圖像拼接功能
這篇文章主要為大家詳細(xì)介紹了Python如何使用OpenCV實(shí)現(xiàn)簡單圖像拼接的核心概念與技術(shù)要點(diǎn),主要內(nèi)容包括圖像獲取,處理,特征提取等關(guān)鍵環(huán)節(jié),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2026-05-05
python 偷懶技巧——使用 keyboard 錄制鍵盤事件
這篇文章主要介紹了python如何使用 keyboard 錄制鍵盤事件,幫助大家提高工作效率,感興趣的朋友可以了解下2020-09-09
Python實(shí)現(xiàn)刪除Android工程中的冗余字符串
這篇文章主要介紹了Python實(shí)現(xiàn)刪除Android工程中的冗余字符串,本文實(shí)現(xiàn)的是刪除Android資源(語言)國際化機(jī)制中的一些冗余字符串,需要的朋友可以參考下2015-01-01
python 如何將office文件轉(zhuǎn)換為PDF
這篇文章主要介紹了python 如何將office文件轉(zhuǎn)換為PDF,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09
TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn)
這篇文章主要介紹了TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

