且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python和Matplotlib,剧情多线(阵列)和动画

更新时间:2023-11-09 20:49:22

作为@Rutger Kassies在评论中指出,

as @Rutger Kassies points out in the comments,

dline = plot(xx,data)

确实对输入数据一些神奇的解析,分开你的阵列成一束的x-y对,并绘制的。需要注意的是鼎联的Line2D 对象的列表的。在这种情况下

does some magic parsing on the input data, separates your arrays into a bunch of x-y pairs and plots those. Note that dline is a list of Line2D objects. In this case

mline, = plot([],[])
mline.set_data(xx.T,data.T)

您正在创建一个的Line2D 对象和库做这是***的推二维数据,到一维绘制对象,并通过扁平化输入这样做。

you are creating a single Line2D object and the library does it's best to shove 2D data, into a 1D plotting objects and does so by flattening the input.

要动画 N 行,你只需要 N 的Line2D 对象:

To animate N lines, you just need N Line2D objects:

lines = [plot([],[])[0] for j in range(Ny)] # make a whole bunch of lines

def init():
    for mline in lines:
        mline.set_data([],[])
    return lines

def animate(coef):
   data = odata * (1.-float(coef)/360.)
   for mline, x, d in zip(lines, data.T, xx.T):
       mline.set_data(x, d)
   return lines

您也不必为pre分配数据和蟒蛇做环比让 numpy的慢得多code>做你的问题。

You also don't need to pre-allocate data and doing the loops in python is much slower than letting numpy do them for you.