|  | 
|  | 1 | +# Author: Listen | 
|  | 2 | +# !/usr/bin/env python3 | 
|  | 3 | +# -*- coding:utf-8 -*- | 
|  | 4 | + | 
|  | 5 | +import numpy as np | 
|  | 6 | +import matplotlib.pyplot as plt | 
|  | 7 | +import mpl_toolkits.mplot3d.axes3d as p3 | 
|  | 8 | +import matplotlib.animation as animation | 
|  | 9 | + | 
|  | 10 | +# Fixing random state for reproducibility | 
|  | 11 | +np.random.seed(19680801) | 
|  | 12 | + | 
|  | 13 | +def Gen_RandLine(length, dims=2): | 
|  | 14 | +    """ | 
|  | 15 | +    Creat a line using a random walk algorithm | 
|  | 16 | + | 
|  | 17 | +    Length is the number of points for the line. | 
|  | 18 | +    dims is the number of dimensions the line has. | 
|  | 19 | + | 
|  | 20 | +    :param length: | 
|  | 21 | +    :param dims: | 
|  | 22 | +    :return: | 
|  | 23 | +    """ | 
|  | 24 | +    lineData = np.empty((dims, length)) | 
|  | 25 | +    lineData[:, 0] = np.random.rand(dims) | 
|  | 26 | +    for index in range(1, length): | 
|  | 27 | +        # scaling the random number by 0.1 so | 
|  | 28 | +        # movement is small compared to position. | 
|  | 29 | +        # subtraction by 0.5 is to change the range to [-0.5, 0.5] | 
|  | 30 | +        # to allow a line to move backwards. | 
|  | 31 | +        step = ((np.random.rand(dims) - 0.5) * 0.1) | 
|  | 32 | +        lineData[:, index] = lineData[:, index - 1] + step | 
|  | 33 | + | 
|  | 34 | +    return lineData | 
|  | 35 | + | 
|  | 36 | +def update_lines(num, dataLines, lines): | 
|  | 37 | +    for line, data in zip(lines, dataLines): | 
|  | 38 | +        # Note: there is no ._setdata() for 3 dim data... | 
|  | 39 | +        line.set_data(data[0:2, :num]) | 
|  | 40 | +        line.set_3d_properties(data[2, :num]) | 
|  | 41 | +    return lines | 
|  | 42 | + | 
|  | 43 | +# Attaching 3D axis to the figure | 
|  | 44 | +fig = plt.figure() | 
|  | 45 | +ax = p3.Axes3D(fig) | 
|  | 46 | + | 
|  | 47 | +# Fifty lines of random 3-D lines | 
|  | 48 | +data = [Gen_RandLine(25, 3) for index in range(50)] | 
|  | 49 | + | 
|  | 50 | +# Creating fifty line objects. | 
|  | 51 | +# Note: Can't pass empty arrays into 3d version of plot() | 
|  | 52 | +lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data] | 
|  | 53 | + | 
|  | 54 | +# Setting the axes properties | 
|  | 55 | +ax.set_xlim3d([0.0, 1.0]) | 
|  | 56 | +ax.set_xlabel('X') | 
|  | 57 | + | 
|  | 58 | +ax.set_ylim3d([0.0, 1.0]) | 
|  | 59 | +ax.set_ylabel('Y') | 
|  | 60 | + | 
|  | 61 | +ax.set_zlim3d([0.0, 1.0]) | 
|  | 62 | +ax.set_zlabel('Z') | 
|  | 63 | + | 
|  | 64 | +ax.set_title('3D Test') | 
|  | 65 | + | 
|  | 66 | +# Creating the Animation object | 
|  | 67 | +line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs=(data, lines), | 
|  | 68 | +                                   interval=50, blit=False) | 
|  | 69 | +line_ani.save('../savefig/3D_animation.gif', writer='imagemagick', fps=30, dpi=40) | 
|  | 70 | +plt.show(fig) | 
0 commit comments