Skip to content

Instantly share code, notes, and snippets.

@Especuloide
Last active August 6, 2021 19:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Especuloide/8334a9bf26c3785b3624bf9987caa92a to your computer and use it in GitHub Desktop.
Save Especuloide/8334a9bf26c3785b3624bf9987caa92a to your computer and use it in GitHub Desktop.
Multi lines animation
import numpy as np
import matplotlib.animation as animation
from matplotlib import pyplot
from random import randint
fig, g = pyplot.subplots(figsize=(6,6))
pyplot.subplots_adjust(left = 0.0, right = 1.0, top = 1.0, bottom = 0.0)
bck = "black" # Background color
pyplot.rcParams['axes.facecolor'] = bck
pyplot.rcParams['savefig.facecolor'] = bck
def circ (r) : # Circunference points
Ex = []
Ey = []
for x in range(0,r) :
Ex.append(np.cos(2*np.pi/r*x))
Ey.append(np.sin(2*np.pi/r*x))
return Ex,Ey
def draw_l(hr, vr, nu): # Generates the points to draw the lines
l = []
dv = 50 # A small marging to avoid points too much close the the edge
for r in range(0,nu) :
x = randint(dv,hr-dv)
y = randint(dv,vr-dv)
d = randint(0,30) # Initital position
l.append([x, y, d])
return l
ctd = 0 # Counter
fr = 29 # Number of frames
px,py = circ(fr) # Calling the circular function to make the coordinates
# Enlarging coordinates the size (to avoid run out of list's points)
px += px*2
py += py*2
# Resolution (600 x 600)
hr = 600
vr = 600
ls = draw_l(hr,vr,30) # Calling the function to get 30 points at 600 x 600 dots resolution
def update(*args) :
global ctd
pyplot.clf()
s = 20 # Movement intensity (circle's size multiplier)
for i1 in range(0,len(ls)) :
for i2 in range(i1,len(ls)) :
# Line parameters
ca = [ ls[i1][0] + px[ ctd+ls[i1][2] ]*s, ls[i2][0] + px[ ctd+ls[i2][2] ]*s ]
cb = [ ls[i1][1] + py[ ctd+ls[i1][2] ]*s, ls[i2][1] + py[ ctd+ls[i2][2] ]*s ]
pyplot.plot( ca, cb, c = "red", alpha = 0.9, linewidth = 0.5)
ctd +=1
pyplot.grid(False)
pyplot.axis("OFF")
pyplot.xlim(0,hr)
pyplot.ylim(0,vr)
return ()
def init() :
return()
anim = animation.FuncAnimation(fig, update, init_func=init, blit = True, frames=fr, repeat = False)
sf = 'Lines.gif'
Sname = 'C:\\Users\\rober\\Desktop\\_' + sf
anim.save(Sname, writer='imagemagick', fps=28)
# You must have imagemagick installed in you computer (in C:\program files\) :
# https://www.imagemagick.org/script/index.php
@Especuloide
Copy link
Author

_Lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment