教你用Python matplotlib庫制作簡單的動畫
動畫即是在一段時間內(nèi)快速連續(xù)的重新繪制圖像的過程.
matplotlib提供了方法用于處理簡單動畫的繪制:
import matplotlib.animation as madef update(number): pass# 每隔30毫秒,執(zhí)行一次updatema.FuncAnimation( mp.gcf(), # 作用域當(dāng)前窗體 update, # 更新函數(shù)的函數(shù)名 interval=30 # 每隔30毫秒,執(zhí)行一次update) 案例1:
隨機(jī)生成各種顏色的100個氣泡, 讓他們不斷增大.
1.隨機(jī)生成100個氣泡.
2.每個氣泡擁有四個屬性: position, size, growth, color
3.把每個氣泡繪制到窗口中.
4.開啟動畫,在update函數(shù)中更新每個氣泡的屬性并重新繪制
'''簡單動畫1. 隨機(jī)生成100個氣泡.2. 每個氣泡擁有四個屬性: position, size, growth, color3. 把每個氣泡繪制到窗口中.4. 開啟動畫,在update函數(shù)中更新每個氣泡的屬性并重新繪制'''import numpy as npimport matplotlib.pyplot as mpimport matplotlib.animation as man = 100balls = np.zeros(n, dtype=[(’position’, float, 2), # 位置屬性(’size’, float, 1), # 大小屬性(’growth’, float, 1), # 生長速度(’color’, float, 4)]) # 顏色屬性# 初始化每個泡泡# uniform: 從0到1取隨機(jī)數(shù),填充n行2列的數(shù)組balls[’position’]=np.random.uniform(0,1,(n,2))balls[’size’]=np.random.uniform(50,70,n)balls[’growth’]=np.random.uniform(10,20,n)balls[’color’]=np.random.uniform(0,1,(n,4))# 繪制100個泡泡mp.figure(’Bubble’, facecolor=’lightgray’)mp.title(’Bubble’, fontsize=18)mp.xticks([])mp.yticks([])sc = mp.scatter(balls[’position’][:,0],balls[’position’][:,1], balls[’size’],color=balls[’color’])# 啟動動畫def update(number):balls[’size’] += balls[’growth’]# 讓某個泡泡破裂,從頭開始執(zhí)行boom_i = number % nballs[boom_i][’size’] = 60balls[boom_i][’position’]= np.random.uniform(0, 1, (1, 2))# 重新設(shè)置屬性sc.set_sizes(balls[’size’])sc.set_offsets(balls[’position’])anim = ma.FuncAnimation(mp.gcf(), update, interval=30)mp.show()
'''模擬心電圖'''import numpy as npimport matplotlib.pyplot as mpimport matplotlib.animation as mamp.figure(’Signal’, facecolor=’lightgray’)mp.title(’Signal’, fontsize=16)mp.xlim(0, 10)mp.ylim(-3, 3)mp.grid(linestyle=’:’)pl = mp.plot([],[], color=’dodgerblue’,label=’Signal’)[0]# 啟動動畫def update(data):t, v = datax, y = pl.get_data() #x y: ndarray數(shù)組x = np.append(x, t)y = np.append(y, v)# 重新繪制圖像pl.set_data(x, y)# 移動坐標(biāo)軸if x[-1]>5:mp.xlim(x[-1]-5, x[-1]+5)x = 0def generator():global xy = np.sin(2 * np.pi * x) * np.exp(np.sin(0.2 * np.pi * x))yield (x, y)x += 0.05anim = ma.FuncAnimation(mp.gcf(), update, generator, interval=30)mp.show()
到此這篇關(guān)于教你用Python matplotlib制作簡單的動畫的文章就介紹到這了,更多相關(guān)matplotlib制作動畫內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. jsp實現(xiàn)登錄驗證的過濾器2. ASP中實現(xiàn)字符部位類似.NET里String對象的PadLeft和PadRight函數(shù)3. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進(jìn))4. 微信開發(fā) 網(wǎng)頁授權(quán)獲取用戶基本信息5. JavaWeb Servlet中url-pattern的使用6. asp批量添加修改刪除操作示例代碼7. 詳解瀏覽器的緩存機(jī)制8. HTML5 Canvas繪制圖形從入門到精通9. css代碼優(yōu)化的12個技巧10. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法
