[Tutor] hi

Vick vick1975 at orange.mu
Wed Aug 7 23:43:24 CEST 2013


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.benjamin at gmail.com] 
Sent: Tuesday, 06 August, 2013 18:40
To: Vick
Cc: tutor at python.org
Subject: Re: [Tutor] hi

On 1 August 2013 12:32, Vick <vick1975 at 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
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test3d.py
URL: <http://mail.python.org/pipermail/tutor/attachments/20130808/5501a9aa/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: testanimpy.py
URL: <http://mail.python.org/pipermail/tutor/attachments/20130808/5501a9aa/attachment-0001.ksh>


More information about the Tutor mailing list