[Tutor] hi

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Aug 6 16:40:22 CEST 2013


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


More information about the Tutor mailing list