環境介紹 python3.8 numpy matplotlib 一、繪製一個三維的愛心 關於這一步,我採用的是大佬博客中的最後一種繪製方法。當然,根據我的代碼習慣,我還是稍做了一點點修改的。 class Guess: def __init__(self, bbox=(-1.5, 1.5), reso ...
環境介紹
python3.8
numpy
matplotlib
一、繪製一個三維的愛心
關於這一步,我採用的是大佬博客中的最後一種繪製方法。當然,根據我的代碼習慣,我還是稍做了一點點修改的。
class Guess: def __init__(self, bbox=(-1.5, 1.5), resolution=50, lines=20) -> None: """ bbox: 控制畫格的大小 resolution: 控制愛心的解析度 lines: 控制等高線的數量 """ self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax = bbox*3 A = np.linspace(self.xmin, self.xmax, resolution) self.B = np.linspace(self.xmin, self.xmax, lines) self.A1, self.A2 = np.meshgrid(A, A) def coordinate(self, x, y, z): """ 生成坐標 """ return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3 def draw(self, ax): """ 繪製坐標 """ for z in self.B: X, Y = self.A1, self.A2 Z = self.coordinate(X, Y, z)+z cset = ax.contour(X, Y, Z, [z], zdir='z', colors=('pink',)) for y in self.B: X, Z = self.A1, self.A2 Y = self.coordinate(X, y, Z)+y cset = ax.contour(X, Y, Z, [y], zdir='y', colors=('pink',)) for x in self.B: Y, Z = self.A1, self.A2 X = self.coordinate(x, Y, Z) + x cset = ax.contour(X, Y, Z, [x], zdir='x', colors=('pink',)) def run(self): fig = plt.figure() ax = fig.add_subplot(projection='3d') ax.set_zlim3d(self.zmin, self.zmax) ax.set_xlim3d(self.xmin, self.xmax) ax.set_ylim3d(self.ymin, self.ymax) plt.show()
但是這可以達到我們想要的效果嗎?
顯然不能!於是我們開始加入億點點細節!
二、細節點
1.加入時間序列
想要心臟跳起來,我們就需要有時間維度的變化。那怎麼做最合理呢?這裡僅展示修改的代碼位置。
代碼如下(示例):
class Guess: def __init__(self, bbox=(-1.5, 1.5), resolution=50, lines=20) -> None: plt.ion() # 開啟畫布的動態圖模式 self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax = bbox*3 self.time = time.time() # 這裡有一個衡量的時間坐標,很合理吧 A = np.linspace(self.xmin, self.xmax, resolution) self.B = np.linspace(self.xmin, self.xmax, lines) self.A1, self.A2 = np.meshgrid(A, A) def run(self, count): """ 加入count是我們想迴圈的次數 """ fig = plt.figure() for i in range(count): plt.clf() # 每次清除畫布 ax = fig.add_subplot(projection='3d') ax.set_zlim3d(self.zmin, self.zmax) ax.set_xlim3d(self.xmin, self.xmax) ax.set_ylim3d(self.ymin, self.ymax) times = time.time()-self.t/ime # 計算畫布的當前時間狀態 self.draw(ax, coef) plt.show()
2.加入心髒的跳動
心髒的跳動當然不會是線性的了,我們需要心髒的跳動是有層次感的,並且還是可以做往返運動的。
emmmmm… 這麼說來,cos是不是就是做這個用的?
於是…
代碼如下(示例):
def __init__(self, bbox=(-1.5, 1.5), resolution=50, lines=20, scale=1.2) -> None: """ scale: 心臟縮放的繫數 """ self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax = bbox*3 plt.ion() self.scale = scale # scale: 心臟縮放的繫數 設置為全局變數 self.time = time.time() A = np.linspace(self.xmin, self.xmax, resolution) self.B = np.linspace(self.xmin, self.xmax, lines) self.A1, self.A2 = np.meshgrid(A, A) def draw(self, ax, coef): """ coef: 使得心臟可以按照時間跳動 """ for z in self.B: X, Y = self.A1, self.A2 Z = self.coordinate(X, Y, z)+z cset = ax.contour(X * coef, Y * coef, Z * coef, [z * coef], zdir='z', colors=('pink',)) for y in self.B: X, Z = self.A1, self.A2 Y = self.coordinate(X, y, Z)+y cset = ax.contour(X * coef, Y * coef, Z * coef, [y * coef], zdir='y', colors=('pink',)) for x in self.B: Y, Z = self.A1, self.A2 X = self.coordinate(x, Y, Z) + x cset = ax.contour(X * coef, Y * coef, Z * coef, [x * coef], zdir='x', colors=('pink',)) def run(self, count): """ 加入count是我們想迴圈的次數 """ fig = plt.figure() for i in range(count): plt.clf() # 每次清除畫布 ax = fig.add_subplot(projection='3d') ax.set_zlim3d(self.zmin, self.zmax) ax.set_xlim3d(self.xmin, self.xmax) ax.set_ylim3d(self.ymin, self.ymax) times = time.time()-self.time coef = np.cos(times) * (self.scale-1) + 1 # coef 是用來放縮心髒的大小的,加入cos來使它有節奏的跳動 self.draw(ax, coef) plt.pause(0.01) plt.show()
很好,這樣我們就有了一個可以跳動的心臟,那麼到這結束了嘛?
一個好的展示
當然沒有!我們希望對象看到的時候他稍微有點東西,所以讓它跳動卻不能改變方向,豈不是看的不夠全面?所以我們在加最後億點點細節:
def run(self, count): fig = plt.figure() for i in range(count): plt.clf() ax = fig.add_subplot(projection='3d') ax.set_title("你對象的名字?") # 加上你對象的小name ax.set_zlim3d(self.zmin, self.zmax) ax.set_xlim3d(self.xmin, self.xmax) ax.set_ylim3d(self.ymin, self.ymax) times = time.time()-self.time ax.view_init(10, 100+np.cos(times) * 10) # 讓三維坐標圖可以變換坐標展示 coef = np.cos(times) * (self.scale-1) + 1 self.draw(ax, coef) plt.pause(0.01) # 讓繪製出來的心臟可以顯示 plt.show()
完整代碼
import time import numpy as np import matplotlib.pyplot as plt class Guess: def __init__(self, bbox=(-1.5, 1.5), resolution=50, lines=20, scale=1.2) -> None: self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax = bbox*3 plt.ion() self.scale = scale self.time = time.time() A = np.linspace(self.xmin, self.xmax, resolution) self.B = np.linspace(self.xmin, self.xmax, lines) self.A1, self.A2 = np.meshgrid(A, A) Python交流群:748989764 def coordinate(self, x, y, z): return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3 def draw(self, ax, coef): for z in self.B: X, Y = self.A1, self.A2 Z = self.coordinate(X, Y, z)+z cset = ax.contour(X * coef, Y * coef, Z * coef, [z * coef], zdir='z', colors=('pink',)) for y in self.B: X, Z = self.A1, self.A2 Y = self.coordinate(X, y, Z)+y cset = ax.contour(X * coef, Y * coef, Z * coef, [y * coef], zdir='y', colors=('pink',)) for x in self.B: Y, Z = self.A1, self.A2 X = self.coordinate(x, Y, Z) + x cset = ax.contour(X * coef, Y * coef, Z * coef, [x * coef], zdir='x', colors=('pink',)) def run(self, count): fig = plt.figure() for i in range(count): plt.clf() ax = fig.add_subplot(projection='3d') ax.set_title("2LiuYu") ax.set_zlim3d(self.zmin, self.zmax) ax.set_xlim3d(self.xmin, self.xmax) ax.set_ylim3d(self.ymin, self.ymax) times = time.time()-self.time ax.view_init(10, 100+np.cos(times) * 10) coef = np.cos(times) * (self.scale-1) + 1 self.draw(ax, coef) plt.pause(0.01) plt.show() if __name__ == '__main__': demo = Guess() demo.run(1000)