2014-01-02 3 views
0

Я читал отличный кусок Джейк Vanderplas по коду для построения точки, которая движется по синусоиде:Matplotlib.animation: отображать точки после построения графика?

""" 
This short code snippet utilizes the new animation package in 
matplotlib 1.1.0; it's the shortest snippet that I know of that can 
produce an animated plot in python. I'm still hoping that the 
animate package's syntax can be simplified further. 
""" 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

def simData(): 
# this function is called as the argument for 
# the simPoints function. This function contains 
# (or defines) and iterator---a device that computes 
# a value, passes it back to the main program, and then 
# returns to exactly where it left off in the function upon the 
# next call. I believe that one has to use this method to animate 
# a function using the matplotlib animation package. 
# 
    t_max = 10.0 
    dt = 0.05 
    x = 0.0 
    t = 0.0 
    while t < t_max: 
     x = np.sin(np.pi*t) 
     t = t + dt 
     yield x, t 

def simPoints(simData): 
    x, t = simData[0], simData[1] 
    time_text.set_text(time_template%(t)) 
    line.set_data(t, x) 
    return line, time_text 

## 
## set up figure for plotting: 
## 
fig = plt.figure() 
ax = fig.add_subplot(111) 
# I'm still unfamiliar with the following line of code: 
line, = ax.plot([], [], 'bo', ms=10) 
ax.set_ylim(-1, 1) 
ax.set_xlim(0, 10) 
## 
time_template = 'Time = %.1f s' # prints running simulation time 
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) 
## Now call the animation package: (simData is the user function 
## serving as the argument for simPoints): 
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,\ 
    interval=10, repeat=True) 
plt.show() 

Как бы вы изменить этот код так, что каждая точка (т.е. расчетное значение х, у) будет оставаться на рисунке после графика? Таким образом, к концу анимации вы увидите все предыдущие точки, формирующие кривую синуса.

+1

На самом деле, я понял! Что я изменил в http://stackoverflow.com/questions/16842770/animation-with-matplotlib-where-points-are-dynamically-added-to-a-graph?rq=1, где были определены xdata, ydata вне функции (как [], []), а функция их построения просто добавила новые данные в xdata, ydata. – covariance

ответ

0

Проверить эту ссылку для объяснения:

# I'm still unfamiliar with the following line of code: 
line, = ax.plot([], [], 'bo', ms=10) 

Python code. Is it comma operator?


Кроме того, это может не помочь с вашей проблемой построения графиков, но это, кажется, что вы definiting time_template но не используется это определение, если оно не называется глобально после этой точки в вашем скрипте.

Смежные вопросы