在Python动画中播放/暂停/停止和计时器

时间:2015-10-29 14:22:27

标签: python python-3.x animation 3d scatter

我创建了一个完美的动画(见代码)。但是,无法设置添加1)播放/暂停/停止按钮或" onClick()":没有任何反应,动画继续运行。 2)计时器一样,我看不到。是否与3D散射有关?

# -*- coding: utf-8 -*-

"""
Create a random distribution of points (cluster), 
change the color and size of points
move the cluster (r, theta, phi)
This is done iteratively until we close the figure.
===================================================
"""

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

pause = True
def onClick(event):
    global pause
    pause ^= True

# We reset the main parameters of the plot
def update_plot(i, color, size, elev, azim, dist, scat):
    global pause
    # Set colors...
    scat.set_array(color[i])
    # Set sizes...
    scat.set_sizes(size[i])
    # Set elevation annd azimuth...
    ax.view_init(elev=elev[i], azim=azim[i])
    # Set distance...
    ax.dist=dist[i]

    return scat,

# How many frame?
numframes = 500
# How many points?
numpoints = 200

# Initialization the position (x, y, z), the color (c) and the size (s) of the points
mu, sigma = 0, 0.25 # mean and standard deviation
x = np.random.normal(mu, sigma, numpoints)
y = np.random.normal(mu, sigma, numpoints)
z = np.random.normal(mu, sigma, numpoints)
c, s = np.random.random((2, numpoints))

# Definition of the data for the new values for each new plot
color_data = np.random.random((numframes, numpoints))
size_data = 200*np.random.random((numframes, numpoints))
elev_data = np.linspace(0, 360, numframes)
azim_data = np.linspace(0, 360, numframes)
dist_data = np.linspace(50, 1, numframes)

fig = plt.figure()
ax = Axes3D(fig,  axisbg='black')

# We do not want the axis
ax.set_axis_off()

# This is where we plot the cluster
scat = ax.scatter(x, y, z, c=c, s=s, alpha=0.5)
xmin = np.min(x)
xmax = np.max(x)
ax.set_xlim(xmin,xmax)
ax.set_ylim(xmin,xmax)
ax.set_zlim(xmin,xmax)

# This is the animation. In fargs, we provide the data for each new plot.
fig.canvas.mpl_connect('button_press_event', onClick)
ani = animation.FuncAnimation(fig, update_plot, frames=range(numframes),
                                  fargs=(color_data, size_data, 
                                  elev_data, azim_data, dist_data,
                                  scat), blit=False, interval=10, repeat=True)

plt.show()

0 个答案:

没有答案