Skip to content

Instantly share code, notes, and snippets.

@Especuloide
Last active April 15, 2022 15:54
Show Gist options
  • Save Especuloide/6c89f6a20280c90cb1d7a676db4945ad to your computer and use it in GitHub Desktop.
Save Especuloide/6c89f6a20280c90cb1d7a676db4945ad to your computer and use it in GitHub Desktop.
Sine_Wave_Worm
import numpy as np
import matplotlib.animation as animation
from matplotlib import pyplot
# Plot size
hr = 1200
vr = 1200
fig, g = pyplot.subplots(figsize=(hr/100,vr/100))
pyplot.subplots_adjust(top = 1.01, left = -0.01, right = 1.01, bottom = -0.01)
bkg = '#484848' # background color
pyplot.rcParams['axes.facecolor'] = bkg
pyplot.rcParams['savefig.facecolor'] = bkg
cts = 0 # Counter
def update(*args) : # gravar png
global cts
pyplot.clf() # Clean the plot after every frame.
nu = 300 # Number of levels
ex = np.arange(0,nu) # X-axis used to calculate Ey
am = 32 # Wave lenght *
mp = 190 # Multiplier *
Ey = (np.sin(ex / am) * mp) + 170
ex2 = np.arange(0+cts,nu+cts) # X-axis used to calculate Ey2 and Ey3
# this one iterates with the counter
am = 16 # Wave lenght *
mp = 100 # Multiplier *
Ey2 = (np.cos(ex2 / am) * mp)
Ey3 = np.cos(ex2 / 4)
# * This values are determined on trial and error basis
lvy = 538 # Initial Y-axis level
for n in range(0,nu) : # Ploting the levels
px = int(Ey[n]) + 125 # Wave X-axis
sl = abs(Ey2[n])*1.5 # Size variation
pyplot.plot(px, lvy, color = 'white', linewidth = 0.0001, alpha = 0.12,
marker = 'o', markeredgecolor = 'silver', markersize = sl )
lvy -= 1.59 + Ey3[n] / 0.68 # Interval o Y-axis an oscillation given br Ey3
# again adjustes on trial and error basis
pyplot.grid(False)
# Scaling
pyplot.xlim(0,600)
pyplot.ylim(0,600)
cts -=1 # Counter
return ()
def init() :
return()
fr = 50 # Number of frames - Adjusted according with sine wave parameters
o = 1 # Animation control, if not "1" and will plot just one frame - useful when adjusting the
# plot - going over all the frames is very boring and a waste of time
if o == 1 :
anim = animation.FuncAnimation(fig, update, init_func=init, blit = True, frames=fr, repeat = False)
sf = "Sine_Wave_Worm"+".gif"
Sname1 = "C://Users//Rober//Desktop//_" + sf # Change the directory
anim.save(Sname1, writer="ffmpeg", fps=21)
else :
update()
@Especuloide
Copy link
Author

Result (just one frame - the gif file is bigger the gist limit):
Worm

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