Hi,

It just plot one line. How to print several lines?

I've attached your example and I've corrected my own 3d code which works now
but it is not an animation. So how can I plot it using the code you gave me?

By the way, I think you know that I'm trying to plot for n-body problem?
Have you made a python code for the n-body problem? Mine works, but I'm just
having trouble with 3d plotting of the orbits. And also I have searched the
web on how to integrate conservation of energy as you suggested to me, but I
couldn't find any article that talks about 1st order ODEs for energy, and
the annotation is different from mine.

PS: Also how do you make the plot run only once without having to do it over
and over again?

Thanks
Vick

-----Original Message-----
From: Oscar Benjamin [mailto:oscar.j.benja...@gmail.com] 
Sent: Tuesday, 06 August, 2013 18:40
To: Vick
Cc: tutor@python.org
Subject: Re: [Tutor] hi

On 1 August 2013 12:32, Vick <vick1...@orange.mu> wrote:
> Hi,

Hi Vick, sorry I've been away and I've only had a chance to look at this
now.

> As per your request below, I have attached a stand-alone example 
> (test3d.py) of my problem. I am trying to plot in 3D using ion() from a
loop.

Basically don't use ion() for animation: it is intended for interactive use.
Use matplotlib's animation API for animation. See
here:
http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

I've put example scripts below. Both run fine on this computer. You'll want
a recent matplotlib version.

Here's a 2d animation script:

#!/usr/bin/env python
# 2d animation

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

fig = plt.figure(figsize=(5, 5))
ax = fig.add_axes([0.15, 0.15, 0.70, 0.70]) line, = ax.plot([], [],
linewidth=2)

def init():
    ax.set_xlim([-1, 1])
    ax.set_ylim([-1, 1])
    line.set_data([], [])
    return line,

def animate(i):
    t = dt * np.arange(i)
    x = np.cos(omega * t)
    y = np.sin(omega * t)
    line.set_data(x, y)
    return line,

dt = .02 # in seconds
T = 10   # Period (seconds)
omega = 2 * np.pi / T  # 0.1 Hz

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=int(T/dt), interval=int(1000*dt),
blit=True)

plt.show()


And here's a 3d script:

#!/usr/bin/env python
# 3d animation

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(5, 5))
ax = fig.add_axes([0.15, 0.15, 0.70, 0.70], projection='3d') line, =
ax.plot([], [], [], linewidth=2)

def init():
    ax.set_xlim([-1, 1])
    ax.set_ylim([-1, 1])
    ax.set_zlim([0, 10])
    line.set_data([], [])
    return line,

def animate(i):
    t = dt * np.arange(i)
    x = np.cos(omega * t)
    y = np.sin(omega * t)
    z = t
    line.set_data(x, y)
    line.set_3d_properties(z)  # WTF!
    return line,

dt = .02 # in seconds
T = 10   # Duration (seconds)
omega = 2 * np.pi  # 1 Hz

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=int(T/dt), interval=int(1000*dt),
blit=True)

plt.show()




Oscar
from pylab import *
#import pylab
from mpl_toolkits.mplot3d.axes3d import Axes3D
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d


def drange(start, stop, step):
    r = start
    while r < stop:
        yield r
        r += step


a=[]
b=[]
z1=[]
c=[]
d=[]
z2=[]




#plt.ion()

fig = plt.figure(dpi=100)
ax = axes3d.Axes3D(fig)#fig.add_subplot(111, projection = '3d')

t = 0
dt = 1
tn= 50

for i in drange (t+dt, tn,dt):
    aa = sin(i)
    bb = cos(i)
    cc = aa*bb
    aa1 = 1.5 *sin(i)
    bb1 = 1.5*cos(i)
    cc1 = aa1*bb1
    a.append(aa)
    b.append(bb)
    z1.append(cc)
    c.append(aa1)
    d.append(bb1)
    z2.append(cc1)
    #clf()
    #ax.plot(a,b,z1, marker='o', linestyle='None')
    #ax.plot(c,d,z2, marker='o', linestyle='None')
ax.plot(a,b,z1)    
ax.plot(c,d,z2)    
plt.show()

#!/usr/bin/env python
# 2d animation

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

fig = plt.figure(figsize=(5, 5))
ax = fig.add_axes([0.15, 0.15, 0.70, 0.70])
line, = ax.plot([], [], linewidth=2)

def init():
    ax.set_xlim([-1, 1])
    ax.set_ylim([-1, 1])
    line.set_data([], [])
    return line,

def animate(i):
    t = dt * np.arange(i)
    x = np.cos(omega * t)
    y = np.sin(omega * t)
    line.set_data(x, y)
    return line,

dt = .02 # in seconds
T = 10   # Period (seconds)
omega = 2 * np.pi / T  # 0.1 Hz

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=int(T/dt), interval=int(1000*dt), 
blit=True)

plt.show()




#!/usr/bin/env python
# 3d animation

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(5, 5))
ax = fig.add_axes([0.15, 0.15, 0.70, 0.70], projection='3d')
line, = ax.plot([], [], [], linewidth=2)

def init():
    ax.set_xlim([-1, 1])
    ax.set_ylim([-1, 1])
    ax.set_zlim([0, 10])
    line.set_data([], [])
    return line,

def animate(i):
    t = dt * np.arange(i)
    x = np.cos(omega * t)
    y = np.sin(omega * t)
    z = t
    line.set_data(x, y)
    line.set_3d_properties(z)  # WTF!
    return line,

dt = .02 # in seconds
T = 10   # Duration (seconds)
omega = 2 * np.pi  # 1 Hz

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=int(T/dt), interval=int(1000*dt), 
blit=True)

plt.show()




import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(5, 5))
ax = fig.add_axes([0.15, 0.15, 0.70, 0.70], projection='3d')
line, = ax.plot([], [], [], linewidth=2)


def init():
    ax.set_xlim([-1, 1])
    ax.set_ylim([-1, 1])
    ax.set_zlim([-1, 1])
    line.set_data([], [])
    return line,

def animate(i):
    t = dt * np.arange(i)
    x = np.sin(t)
    y = np.cos(t)
    z = x*y
    x1 = 1.5 *np.sin(t)
    y1 = 1.5 *np.cos(t)
    z1 = x*y
    
    line.set_data(x, y)
    line.set_3d_properties(z) 
    return line,

dt = 1 # in seconds
T = 50   # Duration (seconds)
#omega = 2 * np.pi  # 1 Hz

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=int(T/dt), interval=int(100*dt), 
blit=True)

plt.show()
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to