本文介绍了Python和Matplotlib,剧情多线(阵列)和动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在Python(和OOP)编程,但我在的Fortran(90/95)坚实的经验和Matlab的节目。

I'm starting programming in Python (and OOP), but I have a solid experience in Fortran (90/95) and Matlab programming.

我正在开发使用上的Tkinter环境动画的小工具。
该工具的目的是动画的多线(一个阵列,而不是数据的向量)。
下面,我的问题的一个简单的例子。
我不明白为什么的数据绘制的这两种方式的结果是如此不同?

I'm developing a little tool using animation on tkinter environment.The goal of this tool is to animate multi-lines (an array and not a vector of data).Below, a simple example of my problem.I don't understand why the result of these two ways of plotting data are so different ?

from pylab import *

Nx=10
Ny=20

xx   = zeros( ( Nx,Ny) )
data = zeros( ( Nx,Ny) )

for ii in range(0,Nx):
    for jj in range(0,Ny):
        xx[ii,jj]   = ii
        data[ii,jj] = jj


dline = plot(xx,data)

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

show()

如果只绘制鼎联的每一行分开,并用不同的颜色绘制。
如果你只绘制多线的所有线路都连接,并与只有一种颜色。

If you plot only "dline" each line is plotted separately and with a different color.If you plot only "mline" all the lines are linked and with only one color.

我的目标是让动画以多线在每个循环改变数据。
在这里,一个简单的源$ C ​​$ C说明我的目的:

My goal is to make an animation with "mline" changing the data at each loop.Here a simple source code illustrating my purposes :

from pylab import *
from matplotlib import animation

Nx=10
Ny=20

fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)

ax = plt.axes(xlim=(0, Nx), ylim=(0, Ny))

xx   = zeros( ( Nx,Ny) )
data = zeros( ( Nx,Ny) )
odata = zeros( ( Nx,Ny) )

for ii in range(0,Nx):
    for jj in range(0,Ny):
        xx[ii,jj]    = ii
        odata[ii,jj] = jj
        data[ii,jj]  = 0.

#dline = plot(xx,odata)

mline, = plot([],[])

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

def animate(coef):
   for ii in range(0,Nx):
        for jj in range(0,Ny):
            data[ii,jj] = odata[ii,jj] * (1.-float(coef)/360.)

   mline.set_data(xx.T,data.T)
   return mline,

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

plt.show()

我希望我已经清楚地暴露了我的问题。

I hope that I have clearly exposed my problem.

谢谢,
尼古拉斯。

Thanks,Nicolas.

推荐答案

作为@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的做你的问题。

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.

这篇关于Python和Matplotlib,剧情多线(阵列)和动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:32