Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Generates Chaos Game video

Project: NEH
Views: 163
Kernel: Python 3 (Anaconda)
%matplotlib inline
import numpy as np import matplotlib.pyplot as plt from matplotlib import animation, rc from IPython.display import HTML
fig, ax = plt.subplots(); ax.set_xlim((0,1)) ax.set_ylim((0,0.866)) ax.set_aspect(0.866) sct = ax.scatter([],[]) initPoints = [[0,0], [1,0], [0.5,0.866]] data = [] r = 0.5 plt.close('all')
def nextPoint(point,init,choice): x1,y1 = point x2,y2 = init[choice] return (x1*(1-r)+x2*r, y1*(1-r)+y2*r)
N = 1000 start = [0.5,0] data.append(start) for i in range(N-1): k = np.random.randint(0,len(initPoints)) data.append(nextPoint(data[-1],initPoints,k))
def animate(i,data): x = data[i][0] y = data[i][1] array = sct.get_offsets() array = np.vstack([array,[x,y]]) sct.set_offsets(array) return sct,
anim = animation.FuncAnimation(fig, animate, frames=1000, interval=30, blit=True, fargs=(data,), repeat=False)
HTML(anim.to_html5_video())
WARNING: Some output was deleted.