簡體   English   中英

我將動態系統的相空間數據集存儲在文件中,並希望用python實現其二維動畫

[英]I have data set of phase space of a dynamical system stored in a file and in want its 2-D animation in python

我有一個包含動態系統的x和y坐標的數據文件。 我想在python中創建動畫。

我已經嘗試過matplotlib.animation作為動畫。

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

fig = plt.figure()
ax = plt.axes(xlim=(0, 1), ylim=(-.1, .1))

filename = '{}{}'.format("file_1234",".txt")
data1=np.loadtxt(filename)
s1=data1[:,0]
t1=data1[:,1]
line, = ax.plot(s1, t1)
def init():  # only required for blitting to give a clean slate.
    line.set_ydata([np.nan] * len(s1))
    return line,


def animate(i):
    line.set_data(s1[i],t1[i])
    #~ line.set_ydata(t1[i])
    #~ line.set_ydata(t1)
    return line,


ani = animation.FuncAnimation(
    fig, animate, init_func=init, interval=2, blit=True, save_count=50)

plt.show()

我已經嘗試過此代碼,但無法獲得確切的相空間。 請幫助我發現錯誤。

enter code here
import numpy as np   

import matplotlib.pyplot as plt     

import matplotlib.animation as animation                         

fig = plt.figure()                      

ax = plt.axes(xlim=(0, 1), ylim=(-.25, .2))                         

filename = '{}{}'.format("file_1",".txt")

data1=np.loadtxt(filename)


filename = '{}{}'.format("file_2",".txt")      

data2=np.loadtxt(filename)

colors1 = ['b', 'b', ]

colors = ['r', 'r']

lines = sum([ax.plot([], [], [], '-', c=c)
         for c in colors], [])

pts = sum([ax.plot([], [], [], 'o', c=c)
       for c in colors], [])

lines1 = sum([ax.plot([], [], [], '-', c=c)
         for c in colors1], [])

pts1 = sum([ax.plot([], [], [], 'o', c=c)
       for c in colors1], [])

def init():

    for line, pt, line1, pt1 in zip(lines, pts, lines1, pts1):

        line.set_data([], [])

        pt.set_data([], [])

        line1.set_data([], [])

        pt1.set_data([], [])     

    return lines + pts + lines1 + pts1


def animate(i):

    for line, pt, line1, pt1, xi, yi in zip(lines, pts, lines1, pts1, data1, data2):

        x, y = data1[:i].T

        p, q = data2[:i].T

        line.set_data(x, y)
        pt.set_data(x[-1:], y[-1:])
        line1.set_data(p, q)
        pt1.set_data(p[-1:], q[-1:])

    return lines + pts + lines1 + pts1

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=7000, interval=10, blit=True)

Writer = animation.writers['ffmpeg']
writer = Writer(fps=500, metadata=dict(artist='Me'), bitrate=100)
anim.save('video.mp4', writer=writer)
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM