简体   繁体   English

matplotlib中的动画情节,带有double for循环

[英]animation plot in matplotlib with double for loop

I would like to create an animation with matplotlib in python. 我想在python中使用matplotlib创建动画。 As the animated data is function of two parameter (time, number of bodies) I got an idea to use 2 for loops. 由于动画数据是两个参数(时间,物体数)的函数,因此我想到了将2用于循环。 The code is below: 代码如下:

import time
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import pylab as plb

#   set up figure for plotting:
fig = plt.figure()
ax = fig.add_subplot(111)

#    plot limits
ax.set_xlim(-(max(q0) + bodies[-1].L), +(max(q0) + bodies[-1].L))
ax.set_ylim(-(max(q0) + bodies[-1].L), +(max(q0) + bodies[-1].L))

#    colors
colors = ['b', 'g', 'c']


for i_t in range(0, np.shape(t)[1]):    #t is an one column matrix
    for i in range(0, N):
        ax.plot(x_matrix[i_t, i], y_matrix[i_t, i], 's', color=colors[i])
    plt.draw()
plt.show()

The problem is that the code generates me one plot but with all animated points. 问题在于该代码为我生成了一个情节,但包含所有动画点。 I would like to have the animation ploted for one time value (counter i_t) and all bodies (counter i) in one window as a movie so the previous plot is erased at the next time step. 我想在一个窗口中将动画绘制为一个时间值(计数器i_t),并将所有物体(计数器i)作为电影绘制,以便在下一个时间步骤删除上一个图。

I have changed the code you suggested to: 我已将您建议的代码更改为:

for i_t in range(0, np.shape(t)[1]):
    for i in range(0, N):
        ax.clear()
        ax.plot(x_matrix[i_t, i], y_matrix[i_t, i], 's', color=colors[i])
        plt.pause(0.001)
plt.draw()

But then I get an error: 但是然后我得到一个错误:

C:\\Python27\\lib\\site-packages\\matplotlib\\backend_bases.py:2292: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation) C:\\ Python27 \\ lib \\ site-packages \\ matplotlib \\ backend_bases.py:2292:MatplotlibDeprecationWarning:使用默认事件循环,直到实现特定于此GUI的功能warnings.warn(str,mplDeprecation)

That isn't so frightening but when running the code the points that would have to be plotted together (counter i) are plotted apart so it looks like blinkig dots (if you know what I mean). 并不是那么令人恐惧,但是在运行代码时,必须一起绘制的点(计数器i)被分开绘制,因此看起来像是闪烁的点(如果您知道我的意思)。

Any suggestions. 有什么建议么。 I think I am close to the solution, but still need the finishig touch for the code to be working. 我认为我已经接近解决方案,但是仍然需要点缀才能使代码正常工作。 I hope this can be done the way I imagined so I would not have to solve the problem with different approach. 我希望可以按照我的想象来完成此操作,这样就不必用其他方法解决问题了。

I have solved the problem. 我已经解决了问题。 The solution is (if anyone will need it): 解决方案是(如果有人需要):

for i_t in range(0, np.shape(t)[1]):
    ax.clear()
    plt.hold(True)
    #    plot limits
    ax.set_xlim(-(max(q0) + bodies[-1].L), +(max(q0) + bodies[-1].L))
    ax.set_ylim(-(max(q0) + bodies[-1].L), +(max(q0) + bodies[-1].L))
    for i in range(0, N):
        ax.plot(x_matrix[i_t, i], y_matrix[i_t, i], 's', color=colors[0, i])

    plt.pause(0.0001)

I would have posted earlyer, but as a new user I had to wait for 8 hours to answer my own question. 我会早点发布,但是作为新用户,我必须等待8个小时才能回答自己的问题。

Change the last section of your code to this: 将代码的最后一部分更改为:

for i_t in range(np.shape(t)[1]):    #t is an one column matrix
    for i in range(N):
        ax.clear()
        ax.plot(x_matrix[i_t, i], y_matrix[i_t, i], 's', color=colors[i])
        plt.pause(0.001)
# eof

Adapt the animation speed by changing the number in plt.pause(0.001) . 通过更改plt.pause(0.001)的数字来调整动画速度。

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

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