2016-07-09 3 views
0

Ich versuche sin und cos auf demselben Graphen mit Spyder (Python 2.7) zu plotten. Ich bin in der Lage eine winkenden sin Kurve mit diesem Code zu zeichnen:ValueError: zu viele Werte zum Entpacken (sin/cos graph mit Python 2.7)

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) 

# initialization function: plot the background of each frame 
def init(): 
    line.set_data([], []) 
    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)  
    return line, 

# 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) 

Jetzt habe ich versucht, eine cos-Kurve zusammen mit der sin-Kurve hinzuzufügen und ich den Code geändert, indem sie in einer neuen Variable (z) unter der Animation Hinzufügen Funktion zum Zeichnen der Cosinus-Kurve. Dies ist, was ich geändert:

z = np.cos(2 * np.pi * (x - 0.01 * i)) 
line.set_data(x,y,x,z) 

Dies ist der neue Code zusammen ist:

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) 

# initialization function: plot the background of each frame 
def init(): 
    line.set_data([], []) 
    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)) 
    z = np.cos(2 * np.pi * (x - 0.01 * i)) 
    line.set_data(x,y,x,z)  
    return line, 

# 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) 

aber ich erhielt:

Traceback (most recent call last): 
    File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 1290, in _on_timer 
    ret = func(*args, **kwargs) 
    File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 925, in _step 
    still_going = Animation._step(self, *args) 
    File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 784, in _step 
    self._draw_next_frame(framedata, self._blit) 
    File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 803, in _draw_next_frame 
    self._draw_frame(framedata) 
    File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 1106, in _draw_frame 
    self._drawn_artists = self._func(framedata, *self._args) 
    File "C:/Users/Keegan/.xy/startups/sinwavegraph.py", line 28, in animate 
    line.set_data(x,y,x,z)  
    File "C:\Python27\lib\site-packages\matplotlib\lines.py", line 561, in set_data 
    x, y = args 
ValueError: too many values to unpack 

Irgendwelche Vorschläge/Empfehlungen wäre sehr dankbar.

Antwort

0

Man kann nicht set_data zu einer Zeile mit vier Argumente. Du brauchst eine andere Linie. Siehe folgende Lösung:

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)) 
line1, = ax.plot([], [], lw=2) 
line2, = ax.plot([], [], lw=2) 

# initialization function: plot the background of each frame 
def init(): 
    line1.set_data([], []) 
    line2.set_data([], []) 
    return [line1, line2] 

# 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)) 
    z = np.cos(2 * np.pi * (x - 0.01 * i)) 
    line1.set_data(x, y) 
    line2.set_data(x, z) 
    return [line1, line2] 

# 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() 
+0

Das hat funktioniert, danke! –