Seite 1 von 1

3D - Animation

Verfasst: Donnerstag 7. März 2013, 18:06
von Grizzly
Hallo Zusammen,
ich würde gerne einen line-plot im Raum (z.B. irgend eine Trajektorie) als Animation laufen lassen.
Ich finde leider kein passendes "Template", ausser:

http://matplotlib.org/examples/animatio ... danim.html

Und da blicke ich leider (noch) nicht durch...

Wisst ihr eventuell ein leichteres ? Viele Grüße, Grizzly

Re: 3D - Animation

Verfasst: Freitag 8. März 2013, 09:32
von Sr4l
Was macht dir den Probleme? Ich habe es mal auf das nötigste reduziert, ohne Zufallsdaten, ohne mehrer Linien.

Kommentier ruhig mal die Zeile `line_ani` aus und schau dir an was dann passiert.

Code: Alles auswählen

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# koordinaten = [(x1, y1, z1), ...]
koordinaten = [(0,0,0), (1, 0, 0), (1,1,0), (0,1,0), (0,0,1), (1,0,1), (1,1,1)]
# data = [(x1, x2, ...), (y1, y2, ...), (z1, z2, ...)]
data = np.array(zip(*koordinaten))

# erstmal alles zeichen (dann brauch man auch keine bereiche festlegen)
line = ax.plot(data[0, 0:], data[1, 0:], data[2, 0:])[0]



# jetzt kann man die animation anwenden oder auch nicht
def update_line(num, dataLine, line) :
    line.set_data(dataLine[0:2, :num])
    line.set_3d_properties(dataLine[2,:num])
    return line

line_ani = animation.FuncAnimation(fig, update_line, 25, fargs=(data, line), interval=100, blit=False)

# am ende muss der plot gezeigt werden
plt.show()

Re: 3D - Animation

Verfasst: Sonntag 10. März 2013, 17:53
von Grizzly
Vielen Dank, das leuchtet mir soweit ein ! Jetzt muss ich das nur noch auf meinen eigentlichen Code anwenden...
Probleme macht mir dabei folgendes:
Ich habe x,y,z - Werte und lade sie in "koordinaten" und schließlich in "data". Wenn ich mir jetzt den shape anschaue erhalte ich z.B. (1000, 3). Dein Beispiel liefert jedoch (3, 7). Dadurch kann ich dann mit line = ax.plot(data[0:, 1], data[0:, 2], data[0:, 3])[0] leider nicht auf x,y,z zugreifen...

Code: Alles auswählen

koordinaten = [x,y,z]
data = np.array(zip(*koordinaten))
print data.shape
Gibt es eine Möglichkeit data zu invertieren ? Ich habe es mit data[::-1] versucht, es ändert jedoch am shape nichts...

Schöne Grüße, Grizzly

PS: Entschuldige meine verspätete Antwort

Re: 3D - Animation

Verfasst: Sonntag 10. März 2013, 18:15
von Grizzly
Vielleicht ist es am Code doch handlicher... ?

Code: Alles auswählen

# This is the example code 
            
import numpy as np                          # for scientific computing
import matplotlib.pyplot as plt             # for plotting later on
#from mpl_toolkits.mplot3d import Axes3D    # for display in 3D
import mpl_toolkits.mplot3d.axes3d as p3    # better import than from...
import matplotlib.animation as animation    # for the animation

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

dt = 0.5*10e-4                              # timestep
Nt = 10e4                                   # number of timesteps
t_total = Nt * dt                           # simulation time
    
    
def lorenzattractor(x,y,z):
    sigma=10.
    r=28.
    b=8./3.
    x_dot = sigma * (y - x)
    y_dot = r*x - y - x*z
    z_dot = x*y - b*z
    return x_dot, y_dot, z_dot
    
   
x = np.ones(Nt+1)                      # new array in x direction
y = np.ones(Nt+1)                      # new array in y direction
z = np.ones(Nt+1)                      # new array in z direction
    
x[0] = 0.                                  # initial value for x
y[0] = 1.                                  # initial value for y
z[0] = 1.                                  # initial value for z
    
for i in range(int(Nt)):               # timeloop
    
    x_dot, y_dot, z_dot = lorenzattractor(x[i], y[i], z[i])
    x[i + 1] = x[i] + (x_dot * dt)
    y[i + 1] = y[i] + (y_dot * dt)
    z[i + 1] = z[i] + (z_dot * dt)
    
koordinaten = [x,y,z]
data = np.array(zip(*koordinaten))
line = ax.plot(data[0:, 1], data[0:, 2], data[0:, 3])[0]

print data.shape

def update_line(num, dataLine, line) :
    line.set_data(dataLine[0:2, :num])
    line.set_3d_properties(dataLine[2,:num])
    return line

line_ani = animation.FuncAnimation(fig, update_line, 25, fargs=(data, line), interval=100, blit=False)
plt.show()

#------------------------------------------- Animation ---------------------------------------------------  
    
#plt.ion()  
#fig = plt.figure(facecolor='white')                          # plot the stuff
#ax = fig.gca(projection='3d')
#ax.plot(x,y,z)
#ax.set_xlabel("X Axis")
#ax.set_ylabel("Y Axis")
#ax.set_zlabel("Z Axis")

#for angle in range(0, 360):
#    ax.view_init(30, angle)
#    plt.draw()

print "Finished"



Re: 3D - Animation

Verfasst: Sonntag 10. März 2013, 18:42
von Sr4l
Sr4l hat geschrieben:

Code: Alles auswählen

# koordinaten = [(x1, y1, z1), ...]
koordinaten = [(0,0,0), (1, 0, 0), (1,1,0), (0,1,0), (0,0,1), (1,0,1), (1,1,1)]
# data = [(x1, x2, ...), (y1, y2, ...), (z1, z2, ...)]
data = np.array(zip(*koordinaten))
Du hast mich falsch verstanden. koordinaten soll eine liste von koordinaten der Form [(x1, y1, z1), (x2, y2, z2), .....] bzw [Punkt1, Punkt2, Punkt3, .....] wobei Punkt = (x, y, z) ist und x, y, z eine Zahl.

Falls du die 3 Raumkoordnaten schon getrennt hast in die Listen x, y und z, dann veränderst du die data zeile von:

Code: Alles auswählen

data = np.array(zip(*koordinaten))
zu:

Code: Alles auswählen

data = np.array((x, y, z))
*edit*
wenn du etwas etwas drehen willst, warum drehst du dann nicht die listen bei der Erstellung, oder danach?

Re: 3D - Animation

Verfasst: Sonntag 10. März 2013, 21:36
von Grizzly
Hallo, vielen Dank für die Hilfe ! Es funktioniert jetzt auch in meinem Code.
Noch ein Blick in die Dokumentation und Animationen sollten generell kein Problem mehr darstellen...

Beste Grüße,
Grizzly