2016-10-09 3 views
1

Ich habe eine Animation in Python und ich möchte ein Zeitlabel hinzufügen, das aktualisiert wird. Ich habe bereits ein NumPy-Array namens Zeit, und so dachte ich, es wäre so einfach wie das Einfügen der Variablen in das Etikett.Animations-Label in Python aktualisieren

fig, ax = plt.subplots() 
line, = ax.plot([], lw=2) 
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes) 

plotlays, plotcols = [1], ["blue"] 
lines = [] 
for index in range(1): 
    lobj = ax.plot([], [], lw=2, color=plotcols[index])[0] 
    lines.append(lobj) 

def animate(i): 
    xlist = [xvals] 
    ylist = [psisol[i,:].real] 
    time_text.set_text('time = %0.2f' % time[i]) 

    for lnum, line in enumerate(lines): 
     line.set_data(xlist[lnum], ylist[lnum]) 

    return lines 

ich getroffen habe es von Jake Vanderplas Double Pendulum Tutorial here, und auch ich sah an diesem Stackoverflow post. Während das Programm ausgeführt wird, bleibt der Plotbereich grau. Wenn ich den Textcode auskommentiere, läuft das Programm perfekt und grafisch und animiert. Ich bin mir nicht sicher, was ich sonst noch versuchen sollte.

Danke für jede Hilfe.

Antwort

1

Ich habe das Tutorial bearbeitet, von dem Sie ausgegangen sind, um einen animierten Text im Diagramm zu haben. Das nächste Mal, bitte geben Sie reproduzierbaren Code (was ist xvals? psisol? + Es gibt keine Importe)

""" 
Matplotlib Animation Example 

author: Jake Vanderplas 
email: [email protected] 
website: http://jakevdp.github.com 
license: BSD 
Please feel free to use and modify this, but keep the above information. Thanks! 
""" 

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 

# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 
line, = ax.plot([], [], lw=2) 
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes) 

# initialization function: plot the background of each frame 
def init(): 
    line.set_data([], []) 
    time_text.set_text('') 
    return line, 

# animation function. This is called sequentially 
def animate(i): 
    x = np.linspace(0, 2, 1000) 
    y = np.sin(2 * np.pi * (x - 0.01 * i)) 
    line.set_data(x, y) 
    time_text.set_text(str(i)) 
    return tuple([line]) + tuple([time_text]) 

# call the animator. blit=True means only re-draw the parts that have changed. 
anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=200, interval=20, blit=True) 

plt.show() 
Verwandte Themen