繁体   English   中英

如何为子图中的数字设置动画?

[英]How to animate the figures in a Subplots?

我写了一个代码来解释波浪的叠加。 我在下面粘贴我的代码输出 但问题是我只创建静态图......如果我可以为波浪设置动画(在我的代码中: subplot(211) )和subplot(212)的对应结果,事情会变得更有趣。 但到目前为止,我只能在没有子图的情况下制作动画……当我在互联网上研究“使用matplotlib在子图中制作动画”时,我发现结果对我来说不太好理解,并且在这种情况下也与我的代码不同。

任何人都可以在这方面帮助我吗? 如果动画基于我的以下代码结构会更好(当然,对子图动画进行必要的更改是值得赞赏的)。 谢谢你们。

我的代码

#Composition of Waves
import matplotlib as mpl
mpl.rc('text', usetex = True)
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np

plt.gca().set_aspect('equal', adjustable='box')
plt.style.use(['ggplot','dark_background'])

title = 'Composition of Waves'
#Parameters:
#a=Amplitude; w=Angular Frequency; phi = Phase Angle.

#Definition of the function:
def f(t,a,w,phi): 
    y = a*np.sin(w*t + phi)
    return y

t = np.arange(0,4*np.pi,0.001)

def create_plot(ptype):
    y1 = f(t,1,1,1)
    y2 = f(t,2,2,2)
    y = y1 + y2
    if ptype == 'waves':
        plt.plot(t, y1, label='$y=f_1(t)$')
        plt.plot(t, y2, label='$y=f_2(t)$')
    elif ptype == 'composition':
        plt.plot(t, y, label='$Composition$', color= 'm')

plt.figure(1)
plt.subplot(211)                                  
create_plot('waves')
plt.legend()
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
#plt.xlabel('$x$')
plt.ylabel('$y$')
plt.title(title)

plt.subplot(212)
create_plot('composition')
plt.legend()
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xlabel('$t$')
plt.ylabel('$y$')

# Tweak spacing between subplots to prevent labels 
from overlapping
plt.subplots_adjust(hspace=0.5)

plt.savefig('composition_Waves.eps', format='eps', dpi=1000,bbox_inches='tight')

plt.show()

输出代码输出

在这里,我想以不同的wphi为波浪设置动画。

无论您是否有子图,创建动画都没有什么不同。 唯一重要的是保留对Artist对象的引用(在这种情况下,由plt.plot()返回的Line2D对象能够在动画函数中修改它们的属性(数据)。

import matplotlib as mpl
mpl.rc('text', usetex = False)
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np

plt.gca().set_aspect('equal', adjustable='box')
plt.style.use(['ggplot','dark_background'])

title = 'Composition of Waves'
#Parameters:
#a=Amplitude; w=Angular Frequency; phi = Phase Angle.

#Definition of the function:
def f(t,a,w,phi): 
    y = a*np.sin(w*t + phi)
    return y

t = np.arange(0,4*np.pi,0.001)

def create_plot(ptype):
    y1 = f(t,1,1,1)
    y2 = f(t,2,2,2)
    y = y1 + y2
    arts = []
    if ptype == 'waves':
        l1, = plt.plot(t, y1, label='$y=f_1(t)$')
        l2, = plt.plot(t, y2, label='$y=f_2(t)$')
        arts = [l1, l2]
    elif ptype == 'composition':
        l3, = plt.plot(t, y, label='$Composition$', color= 'm')
        arts = [l3]
    return arts ## return the artists created by `plt.plot()`



my_lines = [] ## array to keep track of the Line2D artists
fig = plt.figure(1)
plt.subplot(211)                                  
l = create_plot('waves') 
my_lines += l ## add artists to array
plt.legend()
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
#plt.xlabel('$x$')
plt.ylabel('$y$')
plt.title(title)

plt.subplot(212)
l = create_plot('composition')
my_lines += l
plt.legend()
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xlabel('$t$')
plt.ylabel('$y$')

# Tweak spacing between subplots to prevent labels from overlapping
plt.subplots_adjust(hspace=0.5)

print(my_lines)

def animate(i):
    ## in this examples, i takes values 0-10 by steps of 0.01 (the frames in the animation call)
    ## and will represent the frequency of the 2nd wave in the top subplot
    y1 = f(t,1,1,1)
    y2 = f(t,2,i,2)
    y = y1 + y2

    # update the content of the Line2D objects
    my_lines[1].set_ydata(y2)
    my_lines[2].set_ydata(y)
    return my_lines ## return updated artists

ani = animation.FuncAnimation(fig, animate, frames=np.linspace(0,10,100))

plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM