Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animating Random Walk
#1
Hello. I am trying to animate this very simple 2D random walk program. However, I am not sure what the FuncAnimation function is suppose to do and why it isn't working.

Here is the program:


##################
# 2D Random Walk #
##################

import random
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation

xposition = [0]
yposition = [0]

def random_walk():
polar_angle = random.random()*2*math.pi
radius = random.random()

xposition.append( xposition[-1] + radius*math.cos(polar_angle) )
yposition.append( yposition[-1] + radius*math.sin(polar_angle) )

plt.plot( xposition, yposition, c = 'k' )

animation.FuncAnimation( fig = plt.figure(), func = random_walk(), interval = 1000 )
plt.show()


I found a lot of really complicated and hard to read algorithms when I was searching how to make a simple random walk program. Similarly, when I search how to use the FuncAnimation function, I am bombarded with hard to read code that I cannot understand.

I think that fig is the figure onto which the data will be plotted; func is the function to be called every interval, and interval is the time interval in microseconds between each call. I'm not sure why it doesn't work. I'm also not sure why the code I posted isn't displaying correctly. Perhaps, it's my browser.
Reply
#2
Sorry. I fixed the display.

##################
# 2D Random Walk #
##################

import random
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation

xposition = [0]
yposition = [0]

def random_walk():
    polar_angle = random.random()*2*math.pi
    radius      = random.random()

    xposition.append( xposition[-1] + radius*math.cos(polar_angle) )
    yposition.append( yposition[-1] + radius*math.sin(polar_angle) )

    plt.plot( xposition, yposition, c = 'k' )

animation.FuncAnimation( fig = plt.figure(), func = random_walk(),
                         interval = 1000 )
plt.show()
Reply
#3
Okay. For some reason this worked. I suppose adding i as a functional argument makes sense. However, I don't understand why I have to make the animation.FuncAnimation() a variable.

##################
# 2D Random Walk #
##################

import random
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation

xposition = [0]
yposition = [0]

def random_walk(i):
    polar_angle = random.random()*2*math.pi
    radius      = random.random()

    xposition.append( xposition[-1] + radius*math.cos(polar_angle) )
    yposition.append( yposition[-1] + radius*math.sin(polar_angle) )

    plt.plot( xposition, yposition, c = 'k')

animation = animation.FuncAnimation( fig = plt.figure(), func = random_walk,
                                     interval = 1000 )
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Python & MSGraph - Learning to Walk ITMan020324 2 354 Feb-04-2024, 04:37 PM
Last Post: ITMan020324
  Error when animating 3D plot Tee 4 862 Jul-03-2023, 08:49 PM
Last Post: Tee
  EasySNMP Walk/BulkWalk pylance 3 2,033 Nov-29-2021, 12:00 PM
Last Post: pylance
  How to sort os.walk list? Denial 6 11,293 Oct-10-2020, 05:28 AM
Last Post: Denial
  os.walk question DPaul 2 2,282 May-31-2020, 02:08 PM
Last Post: DPaul
  os.walk(Path("path_string")) giving error Kumarkv 4 3,799 May-10-2020, 08:46 AM
Last Post: snippsat
  os.walk does not see files that are in the folder kiton 1 2,960 Jan-21-2020, 07:26 PM
Last Post: micseydel
  print notifcation when enter new directory os.walk() evilcode1 3 2,581 Jun-20-2019, 08:19 PM
Last Post: evilcode1
  Python3 & os.walk while_e_coyote 2 3,406 Aug-24-2018, 07:32 PM
Last Post: while_e_coyote
  how to test if an object came from os.walk() Skaperen 3 3,151 Jan-10-2018, 03:05 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020