1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.mplot3d.axes3d as p3 import matplotlib.animation as animation
np.random.seed(19680801)
def Gen_RandLine(length, dims=2): """ Create a line using a random walk algorithm
length is the number of points for the line. dims is the number of dimensions the line has. """ lineData = np.empty((dims, length)) lineData[:, 0] = np.random.rand(dims) for index in range(1, length): step = ((np.random.rand(dims) - 0.5) * 0.1) lineData[:, index] = lineData[:, index - 1] + step
return lineData
def update_lines(num, dataLines, lines): for line, data in zip(lines, dataLines): line.set_data(data[0:2, :num]) line.set_3d_properties(data[2, :num]) return lines
fig = plt.figure() ax = p3.Axes3D(fig)
data = [Gen_RandLine(25, 3) for index in range(50)]
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]
ax.set_xlim3d([0.0, 1.0]) ax.set_xlabel('X')
ax.set_ylim3d([0.0, 1.0]) ax.set_ylabel('Y')
ax.set_zlim3d([0.0, 1.0]) ax.set_zlabel('Z')
ax.set_title('3D Test')
line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs=(data, lines), interval=50, blit=False)
plt.show()
|