Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random walk "art"
import random
import matplotlib.animation as animation
from matplotlib import pyplot
from random import randint
fig, g = pyplot.subplots(figsize=(6,6))
pyplot.subplots_adjust(top = 1.0, left = 0.00, right = 1.00, bottom = 0.00)
bck = "#595959" # Background color
pyplot.rcParams['axes.facecolor'] = bck
pyplot.rcParams['savefig.facecolor'] = bck
def stpes(sim,po) : # Walkers steps
global lstep, tf
def mov(c) : # Walkers movements
global tf,lstep
s = randint(500,2000)/1000 # Size of a step
if c == 0 : # step right
tf.append(lstep[-1][0]+s)
tf.append(lstep[-1][1])
if c == 1 : # step left
tf.append(lstep[-1][0]-s)
tf.append(lstep[-1][1])
if c == 2 : # step up
tf.append(lstep[-1][0])
tf.append(lstep[-1][1]+s)
if c == 3 : # step down
tf.append(lstep[-1][0])
tf.append(lstep[-1][1]-s)
return
lstep = [po] # Initial points x and y
for n in range(sim) :
tf = []
c = random.choice([0,1,2,3])
mov(c)
lstep.append(tf)
return lstep
wk = 60 # Number of walkers
ns = 120 # Number of steps
walk = [] # List with walkers and steps
clor = [] # Color list
for n in range(0,wk): # making the list of drunks
# x and y - initial position
x = float(randint(-3,3))
y = float(randint(-3,3))
cm = pyplot.cm.gist_heat # Colormap
cl = randint(0,300) # Choice of colormap´s color
walk.append(stpes(ns,[x,y]))
clor.append(cm(cl))
ctd = 0 # Counter
def update(*args) :
global ctd
pyplot.clf()
pyplot.grid(False)
pyplot.axis("OFF")
# Scale
scl = 20
pyplot.xlim(-scl,scl)
pyplot.ylim(-scl,scl)
for st in range(0,len(walk)): # Plotting the drunks
cl = clor[st]
pyplot.plot(walk[st][ctd][0], walk[st][ctd][1] ,marker = 's', alpha = 0.9, markersize = 2.5, c = cl)
for st in range(0,len(walk)) :
x = []
y = []
cl = clor[st]
for r in range(0, ctd+1) :
x.append(walk[st][r][0])
y.append(walk[st][r][1])
pyplot.plot(x, y, alpha = 0.9, c = cl, linewidth = 0.6)
ctd +=1
if ctd > ns : # Counter can not be higher tha the number of steps
ctd = ns
return ()
def init() :
return()
anim = animation.FuncAnimation(fig, update, init_func=init, blit = True, frames=ns+35, repeat = False)
sf = "Random_Walkers.gif"
Sname = "C:\\Users\\Rober\\Desktop\\_" + sf # Change to your directory
anim.save(Sname, writer="imagemagick", fps=11)
# You must have imagemagick installed in you computer :
# https://www.imagemagick.org/script/index.php
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.