尝试使用分支为不断成长和收缩的细丝设置动画

时间:2019-04-29 23:50:05

标签: python matplotlib animation

基本上,母丝从空间的起始点开始生长和收缩。当达到阈值长度时(在我提供的代码中,它对应于坐标=(X = 8,X = 8))。此时,分支从(8,8)开始增长。分支的增长和收缩与父代相似,不同之处在于分支的起始点是(8,8),而父代的分支是(1,1)(尽管我从(1,1)开始分支,即只是为了使列表的长度等于动画的长度)

更大的问题是,我的代码中有几条这样的父级细丝以不同的角度生长,并且当它们中的每一个越过阈值长度时,都会以随机角度出现分支。同样,如果父级收缩到小于阈值的长度,则分支消失(或者以简便的方式,该分支的坐标与父级相同。)

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

X = [1,2,3,4,5,6,7,8,9,10,11,12,11,10,11,12,13,14,15,14,13,12,13] #parent x coord
Y = [1,2,3,4,5,6,7,8,9,10,11,12,11,10,11,12,13,14,15,14,13,12,13] #parent y coord

X1 = [1,2,3,4,5,6,7,8,7,6,5,4,4,4,5,6,7,8,7,6,5,4,3] #branch x coord
Y1 = [1,2,3,4,5,6,7,8,9,10,11,12,12,12,11,10,9,8,9,10,11,12,13] #branch y coord

fig, ax = plt.subplots(1,1)
ax.set_xlim([0, 20])
ax.set_ylim([-1.1, 20])

graph1, = ax.plot([], [], color = 'green')
graph2, = ax.plot([], [], color = 'green')
dot1, = ax.plot([], [], 'o', color='red', markersize = 5)

dot2, = ax.plot([], [], 'o', color='green', markersize = 5)
def oj(i):

    graph1.set_data([X[0],X[i]],[Y[0],Y[i]]) ## for the parent this works as I want it grow and shrink without any trace

    graph2.set_data(X1[:i+1],Y1[:i+1]) # for the branch I can't use the same code as that for graph1 as I want it to be attached at (8,8) and grow from or shrink to that poin
    dot1.set_data(X[i],Y[i]) # this shows how the tip of the parent filament animates

    dot2.set_data(X1[i],Y1[i]) #this shows the tip of the branch which I face a difficulty in animating as it traces back instead of showing a shrink 
anim = animation.FuncAnimation(fig, oj, frames=len(X), interval=1000, repeat = False)
plt.show()

1 个答案:

答案 0 :(得分:0)

我实际上会保持与主分支相同的策略,但是会引入一个参数,指示分支应该在哪一步发生并从那里开始工作:

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

X = [1,2,3,4,5,6,7,8,9,10,11,12,11,10,11,12,13,14,15,14,13,12,13] #parent x coord
Y = [1,2,3,4,5,6,7,8,9,10,11,12,11,10,11,12,13,14,15,14,13,12,13] #parent y coord

X1 = [7,6,5,4,4,4,5,6,7,8,7,6,5,4,3] #branch x coord
Y1 = [9,10,11,12,12,12,11,10,9,8,9,10,11,12,13] #branch y coord

fig, ax = plt.subplots(1,1)
ax.set_xlim([0, 20])
ax.set_ylim([-1.1, 20])

graph1, = ax.plot([], [], color = 'green')
graph2, = ax.plot([], [], color = 'green')
dot1, = ax.plot([], [], 'o', color='red', markersize = 5)
dot2, = ax.plot([], [], 'o', color='green', markersize = 5)
ttl = ax.set_title('')

def oj(i, branch_point):
    ttl.set_text('i={:d}'.format(i))
    graph1.set_data([X[0],X[i]],[Y[0],Y[i]]) ## for the parent this works as I want it grow and shrink without any trace
    dot1.set_data(X[i],Y[i]) # this shows how the tip of the parent filament animates
    if i>branch_point:
        graph2.set_data([X[branch_point],X1[i-branch_point-1]],[Y[branch_point],Y1[i-branch_point-1]]) # for the branch I can't use the same code as that for graph1 as I want it to be attached at (8,8) and grow from or shrink to that poin
        dot2.set_data(X1[i-branch_point-1],Y1[i-branch_point-1]) #this shows the tip of the branch which I face a difficulty in animating as it traces back instead of showing a shrink 
anim = animation.FuncAnimation(fig, oj, frames=len(X), interval=1000, repeat=False, fargs=(7,))
plt.show()