{"id":1088,"date":"2021-11-12T22:32:48","date_gmt":"2021-11-13T06:32:48","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1088"},"modified":"2021-11-12T22:32:48","modified_gmt":"2021-11-13T06:32:48","slug":"188-create-animation-using-matplotlib","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/188-create-animation-using-matplotlib\/","title":{"rendered":"#188 Create animation using  matplotlib"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Make a movie out of the steps of a 2D random walk.\r\nimport matplotlib.pyplot as plt\r\nfrom matplotlib import animation\r\nfrom numpy.random import random as rand\r\n# Set number of steps for each random walk.\r\nnum_steps = 1000\r\n# Create an empty figure of the desired size.\r\nplt.close('all')\r\n# Clear anything left over from prior runs.\r\nbound = 20\r\nfig = plt.figure()\r\n# Must have figure object for movie.\r\nax = plt.axes(xlim=(-bound, bound), ylim=(-bound, bound))\r\n# Create empty line and point objects with no data.\r\n# They will be updated during each frame of the animation.\r\n(my_line,) = ax.plot(&#x5B;], &#x5B;], lw=2)\r\n# Line to show path\r\n(my_point,) = ax.plot(&#x5B;], &#x5B;], 'ro', ms=9)\r\n# Dot to show current position\r\n# Generate the random walk data.\r\nx_steps = 2*(rand(num_steps) &lt; 0.5) - 1\r\n# Generate random steps +\/- 1.\r\ny_steps = 2*(rand(num_steps) &lt; 0.5) - 1\r\nx_coordinate = x_steps.cumsum()\r\n# Sum steps to get position.\r\ny_coordinate = y_steps.cumsum()\r\n# This function will generate each frame of the animation.\r\n# It adds all of the data through frame n to a line\r\n# and moves a point to the nth position of the walk.\r\ndef get_step(n, x, y, this_line, this_point):\r\n    this_line.set_data(x&#x5B;:n+1], y&#x5B;:n+1])\r\n    this_point.set_data(x&#x5B;n], y&#x5B;n])\r\n\r\n# Call the animator and create the movie.\r\nmy_movie = animation.FuncAnimation(fig, get_step, frames=num_steps,\r\n                                   fargs=(x_coordinate, y_coordinate, my_line, my_point) )\r\n# Install FFMPEG\r\nmy_movie.save('ex188_random_walk.mp4', fps=30)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p># Make a movie out of the steps of a 2D random walk. import matplotlib.pyplot as plt from matplotlib import animation from numpy.random import random as rand # Set number of steps for each random walk. num_steps = 1000 # Create an empty figure of the desired size. plt.close(&#8216;all&#8217;) # Clear anything left over from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1088","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-hy","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2099,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/410-2d-random-walk-animation-in-python\/","url_meta":{"origin":1088,"position":0},"title":"#410 2D random walk animation in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy import random import matplotlib.pyplot as plt from matplotlib import animation # define some plot variables fig, ax = plt.subplots(figsize=(8,8)) bound = 25 ax.set_xlim(-bound,bound) ax.set_ylim(-bound,bound) # define a numpy array to hold the locations visited on the random walk locations = numpy.zeros((1,2)) # 1 row, 2 columns\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex410.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex410.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex410.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1211,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2\/","url_meta":{"origin":1088,"position":1},"title":"#214 Animated 3D random walk","author":"gantovnik","date":"2021-11-28","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # Fixing random state for reproducibility np.random.seed(19680801) def random_walk(num_steps, max_step=0.05): \"\"\"Return a 3D random walk as (num_steps, 3) array.\"\"\" start_pos = np.random.random(3) steps = np.random.uniform(-max_step, max_step, size=(num_steps, 3)) walk = start_pos + np.cumsum(steps, axis=0) return walk\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex214.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex214.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex214.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2094,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/409-rain-drops-animation-using-matplotlib-in-python\/","url_meta":{"origin":1088,"position":2},"title":"#409 Rain drops &#8211; animation using matplotlib in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation # Fixing random state for reproducibility np.random.seed(19680801) # Create new Figure and an Axes which fills it. fig = plt.figure(figsize=(7, 7)) ax = fig.add_axes([0, 0, 1, 1], frameon=False) ax.set_xlim(0, 1), ax.set_xticks([]) ax.set_ylim(0, 1), ax.set_yticks([]) # Create\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2175,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","url_meta":{"origin":1088,"position":3},"title":"#425 An animation of a bouncing ball using python","author":"gantovnik","date":"2024-05-05","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as animation # Acceleration due to gravity, m.s-2. g = 9.81 # The maximum x-range of ball's trajectory to plot. XMAX = 5 # The coefficient of restitution for bounces (-v_up\/v_down). cor = 0.65 # The time\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2065,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/408-animated-line-plot-in-python\/","url_meta":{"origin":1088,"position":4},"title":"#408 Animated line plot in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy as np from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation plt.style.use('seaborn-pastel') fig = plt.figure() n=10 ax = plt.axes(xlim=(0, n), ylim=(-2, 2)) line, = ax.plot([], [], c=\"blue\") def init(): line.set_data([], []) return line, def animate(i): n = 10 x = np.linspace(0, n, 1000) y =\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2171,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/424-an-animation-of-a-decaying-sine-curve-using-python\/","url_meta":{"origin":1088,"position":5},"title":"#424 An animation of a decaying sine curve using python","author":"gantovnik","date":"2024-05-05","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # Time step for the animation (s), max time to animate for (s). dt, tmax = 0.01, 5 # Signal frequency (s-1), decay constant (s-1). f, alpha = 2.5, 1 # These lists will hold the\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex424.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex424.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex424.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=1088"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1088\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}