最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python matplotlib 動畫繪制詳情

 更新時間:2022年09月01日 09:38:17   作者:Chandler_river  
這篇文章主要介紹了Python matplotlib 動畫繪制,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下

最最簡單的操作

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.subplots()
 
x = np.linspace(0,10,100)
y = np.sin(x)
 
while True:
    ax.plot(x,y)
    plt.pause(1)
    ax.cla()      
    x += np.pi/30    
    y = np.sin(x)

有人會問,為什么不能直接 用 plot 替代 ax 呢?

好問題,你可以一試,會發(fā)現(xiàn)這玩意沒法關(guān)掉 。。 當然  ctrl + C等暴力手段是任何時候都ok的

Animation類

FuncAnimation

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure()
ax = fig.subplots()
 
x = np.linspace(0,10,100)
y = np.sin(x)
ax.set_aspect(3)
ax.plot(x,y,'-.',c='red',label="the old one")
line = ax.plot(x,y,c='green')
plt.legend()
 
def fun(i):  
    global x    
    x += 0.1
    y = np.sin(x)
    line[0].set_ydata(y)
    return line
 
 
animation = FuncAnimation(fig,fun,interval=100) 
plt.show() 

這就有兩個問題需要解決一下

第一個:line到底是什么類型的東西

type(line)
<class 'list'>

明顯,這就是。。列表。

第二個:set_data;set_xdata;set_ydata

你可以自己更改一下試試看,結(jié)果是顯而易見的

ArtistAnimation

它的好處是你不要費盡心機去想一個可能 勾八 的函數(shù)了

它的壞處是 :

一個能用函數(shù)表示的動畫 為什么要在新增一個列表才能表達呢?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
fig = plt.figure()
ax = fig.subplots()
 
frames = []
x = np.linspace(0,np.pi*2,10)
for i in range(20):
    x += np.pi*2/20
    y = np.sin(x)
    frames.append(ax.plot(y,'-.',c='red'))
 
animation = ArtistAnimation(fig,frames,interval=100)
plt.show() 

很好!現(xiàn)在只需要保存動畫就圓滿了

動畫保存

.save()函數(shù)

filename畫文件名+后綴
fps動畫每秒的幀數(shù)   默認值為 原動畫的幀數(shù)
dpi動畫每英寸的點數(shù) 默認值為 原動畫的點數(shù)
codec編碼格式 默認值為’h264’

filename畫文件名+后綴fps動畫每秒的幀數(shù)   默認值為 原動畫的幀數(shù)dpi動畫每英寸的點數(shù) 默認值為 原動畫的點數(shù)codec編碼格式 默認值為’h264’

animation.save("1.gif")

到此這篇關(guān)于Python matplotlib 動畫繪制的文章就介紹到這了,更多相關(guān)Python matplotlib 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

龙游县| 广西| 龙游县| 油尖旺区| 新干县| 丹寨县| 本溪市| 广水市| 泗阳县| 霞浦县| 保靖县| 临汾市| 南部县| 固安县| 武城县| 宁陕县| 彭水| 澎湖县| 永兴县| 大港区| 云霄县| 从江县| 高阳县| 旬阳县| 清河县| 思南县| 乐亭县| 上虞市| 抚州市| 如东县| 疏勒县| 布拖县| 南靖县| 高淳县| 万安县| 西乌| 垦利县| 于都县| 万年县| 葫芦岛市| 河间市|