首页 > 解决方案 > 尝试用树枝为不断增长和收缩的细丝设置动画

问题描述

基本上,母细丝从空间的初始点开始生长和收缩。当它达到阈值长度时(在我提供的代码中,它对应于坐标 = (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()

标签: pythonmatplotlibanimation

解决方案


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

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()

推荐阅读