Skip to content

Instantly share code, notes, and snippets.

@kkiningh
Created May 23, 2018 20:21
Show Gist options
  • Save kkiningh/611e364cec1fbdd1f28c5b1aaf9e9aa1 to your computer and use it in GitHub Desktop.
Save kkiningh/611e364cec1fbdd1f28c5b1aaf9e9aa1 to your computer and use it in GitHub Desktop.
def draw_circle_packing(x, D, cmap=plt.cm.Spectral):
T, N, _ = x.shape
R = D/2
# Create axis
fig, ax = plt.subplots(figsize=(5,5))
ax.set(xlim=(-R, 1+R), ylim=(-R, 1+R))
# Create new circle objects
colors = cmap(np.linspace(0, 1, N))
Cs = [Circle(c, r, color=k, fill=False) for c, r, k in zip(x[0, :, :], R, colors)]
for c in Cs:
ax.add_artist(c)
# Add bounding box
ax.add_artist(Rectangle([0, 0], 1, 1, fill=False, linestyle='--'))
ax.set_axis_off()
# Animate
def animate(t):
ax.set_title('Iteration {}'.format(t))
for i, C in enumerate(Cs):
C.center = x[t, i, 0], x[t, i, 1]
return FuncAnimation(fig, animate, interval=100, frames=T)
x = np.random.randn(1, 11, 2) / 10 + np.linspace(0, 1, 20).reshape([20, 1, 1]) + np.random.randn(20, 11, 2) / 50
anim = draw_circle_packing(x, 1/15)
HTML(anim.to_html5_video())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment