日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python animation 轨迹_Matplotlib animation模块实现动态图

發布時間:2024/3/12 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python animation 轨迹_Matplotlib animation模块实现动态图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

matplotlib 畫圖功能非常強大,目前也只能根據官網提供的例子簡單地畫幾張圖。最近學習了能畫動態圖的animation模塊,作個簡單地記錄。

在matplotlib作圖中,比較常用的是matplotlib.pyplot模塊,這個模塊有非常多的屬性和方法,簡要列舉下這次用到的方法:

matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

返回fig和ax對象!

例子1. 動態畫出sin函數曲線

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()

xdata, ydata = [], []

ln, = ax.plot([], [], 'r-', animated=False)

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return ln,

def update(frame):

xdata.append(frame)

ydata.append(np.sin(frame))

ln.set_data(xdata, ydata)

return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),

init_func=init, blit=True)

plt.show()

畫這類圖的關鍵是要給出不斷更新的函數,這里就是update 函數了。注意, line, = ax.plot([], [], 'r-', animated=False) 中的, 表示創建tuple類型。迭代更新的數據frame 取值從frames 取得。

例子2. 動態顯示一個動點,它的軌跡是sin函數。

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

"""

animation example 2

author: Kiterun

"""

fig, ax = plt.subplots()

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

l = ax.plot(x, y)

dot, = ax.plot([], [], 'ro')

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

def gen_dot():

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

yield newdot

def update_dot(newd):

dot.set_data(newd[0], newd[1])

return dot,

ani = animation.FuncAnimation(fig, update_dot, frames = gen_dot, interval = 100, init_func=init)

ani.save('sin_dot.gif', writer='imagemagick', fps=30)

plt.show()

這里我們把生成的動態圖保存為gif圖片,前提要預先安裝imagemagic。

例子3. 單擺(沒阻尼&有阻尼)

無阻尼的單擺力學公式:

附加阻尼項:

這里需要用到scipy.integrate的odeint模塊,具體用法找時間再專門寫一篇blog吧,動態圖代碼如下:

# -*- coding: utf-8 -*-

from math import sin, cos

import numpy as np

from scipy.integrate import odeint

import matplotlib.pyplot as plt

import matplotlib.animation as animation

g = 9.8

leng = 1.0

b_const = 0.2

# no decay case:

def pendulum_equations1(w, t, l):

th, v = w

dth = v

dv = - g/l * sin(th)

return dth, dv

# the decay exist case:

def pendulum_equations2(w, t, l, b):

th, v = w

dth = v

dv = -b/l * v - g/l * sin(th)

return dth, dv

t = np.arange(0, 20, 0.1)

track = odeint(pendulum_equations1, (1.0, 0), t, args=(leng,))

#track = odeint(pendulum_equations2, (1.0, 0), t, args=(leng, b_const))

xdata = [leng*sin(track[i, 0]) for i in range(len(track))]

ydata = [-leng*cos(track[i, 0]) for i in range(len(track))]

fig, ax = plt.subplots()

ax.grid()

line, = ax.plot([], [], 'o-', lw=2)

time_template = 'time = %.1fs'

time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)

def init():

ax.set_xlim(-2, 2)

ax.set_ylim(-2, 2)

time_text.set_text('')

return line, time_text

def update(i):

newx = [0, xdata[i]]

newy = [0, ydata[i]]

line.set_data(newx, newy)

time_text.set_text(time_template %(0.1*i))

return line, time_text

ani = animation.FuncAnimation(fig, update, range(1, len(xdata)), init_func=init, interval=50)

#ani.save('single_pendulum_decay.gif', writer='imagemagick', fps=100)

ani.save('single_pendulum_nodecay.gif', writer='imagemagick', fps=100)

plt.show()

例子4. 滾動的球

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

fig = plt.figure(figsize=(6, 6))

ax = plt.gca()

ax.grid()

ln1, = ax.plot([], [], '-', lw=2)

ln2, = ax.plot([], [], '-', color='r', lw=2)

theta = np.linspace(0, 2*np.pi, 100)

r_out = 1

r_in = 0.5

def init():

ax.set_xlim(-2, 2)

ax.set_ylim(-2, 2)

x_out = [r_out*np.cos(theta[i]) for i in range(len(theta))]

y_out = [r_out*np.sin(theta[i]) for i in range(len(theta))]

ln1.set_data(x_out, y_out)

return ln1,

def update(i):

x_in = [(r_out-r_in)*np.cos(theta[i])+r_in*np.cos(theta[j]) for j in range(len(theta))]

y_in = [(r_out-r_in)*np.sin(theta[i])+r_in*np.sin(theta[j]) for j in range(len(theta))]

ln2.set_data(x_in, y_in)

return ln2,

ani = animation.FuncAnimation(fig, update, range(len(theta)), init_func=init, interval=30)

ani.save('roll.gif', writer='imagemagick', fps=100)

plt.show()

到此這篇關于Matplotlib animation模塊實現動態圖 的文章就介紹到這了,更多相關Matplotlib 動態圖 內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

總結

以上是生活随笔為你收集整理的python animation 轨迹_Matplotlib animation模块实现动态图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。