{"id":2065,"date":"2024-01-14T06:02:42","date_gmt":"2024-01-14T14:02:42","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2065"},"modified":"2024-01-14T19:10:15","modified_gmt":"2024-01-15T03:10:15","slug":"408-animated-line-plot-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/408-animated-line-plot-in-python\/","title":{"rendered":"#408 Animated line plot in python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/408-animated-line-plot-in-python\/ex408-2\/\" rel=\"attachment wp-att-2067\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-2067\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nfrom matplotlib import pyplot as plt\r\nfrom matplotlib.animation import FuncAnimation\r\n\r\nplt.style.use('seaborn-pastel')\r\n\r\nfig = plt.figure()\r\nn=10\r\nax = plt.axes(xlim=(0, n), ylim=(-2, 2))\r\nline, = ax.plot(&#x5B;], &#x5B;], c=&quot;blue&quot;)\r\n\r\ndef init():\r\n    line.set_data(&#x5B;], &#x5B;])\r\n    return line,\r\n\r\ndef animate(i):\r\n    n = 10\r\n    x = np.linspace(0, n, 1000)\r\n    y = np.sin(np.pi * (x - 0.01 * i)) * x\/n\r\n    line.set_data(x, y)\r\n    return line,\r\n\r\nanimation = FuncAnimation(fig, animate, init_func=init,\r\n                               frames=200, interval=20, blit=True)\r\n\r\nanimation.save('ex408.gif', writer='imagemagick')\r\nplt.show()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation plt.style.use(&#8216;seaborn-pastel&#8217;) fig = plt.figure() n=10 ax = plt.axes(xlim=(0, n), ylim=(-2, 2)) line, = ax.plot(&#x5B;], &#x5B;], c=&quot;blue&quot;) def init(): line.set_data(&#x5B;], &#x5B;]) return line, def animate(i): n = 10 x = np.linspace(0, n, 1000) y = np.sin(np.pi * (x &#8211; 0.01 * i)) [&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":"off","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_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":"","jetpack_post_was_ever_published":false},"categories":[89,69,2],"tags":[],"class_list":["post-2065","post","type-post","status-publish","format-standard","hentry","category-animation","category-matplotlib","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-xj","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2175,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","url_meta":{"origin":2065,"position":0},"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":2171,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/424-an-animation-of-a-decaying-sine-curve-using-python\/","url_meta":{"origin":2065,"position":1},"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":[]},{"id":2099,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/410-2d-random-walk-animation-in-python\/","url_meta":{"origin":2065,"position":2},"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":2059,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/407-multiple-axes-animation-using-python\/","url_meta":{"origin":2065,"position":3},"title":"#407 Multiple axes animation using python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"y=sin(x) [code language=\"python\"] import matplotlib.pyplot as plt import numpy as np import matplotlib.animation as animation from matplotlib.patches import ConnectionPatch fig, (axl, axr) = plt.subplots( ncols=2, sharey=True, figsize=(6, 2), gridspec_kw=dict(width_ratios=[1, 3], wspace=0), ) axl.set_aspect(1) axr.set_box_aspect(1 \/ 3) axr.yaxis.set_visible(False) axr.xaxis.set_ticks([0, np.pi, 2 * np.pi], [\"0\", r\"$\\pi$\", r\"$2\\pi$\"]) # draw circle with initial\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\/ex407.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex407.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex407.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1088,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/188-create-animation-using-matplotlib\/","url_meta":{"origin":2065,"position":4},"title":"#188 Create animation using  matplotlib","author":"gantovnik","date":"2021-11-12","format":false,"excerpt":"[code language=\"python\"] # 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.\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1109,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/193-animation-using-python\/","url_meta":{"origin":2065,"position":5},"title":"#193 Animation using python","author":"gantovnik","date":"2021-11-19","format":false,"excerpt":"[code language=\"python\"] # create an animation import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as manimation n = 1000 x = np.linspace(0, 6*np.pi, n) y = np.sin(x) # Define the meta data for the movie FFMpegWriter = manimation.writers[\"ffmpeg\"] metadata = dict(title=\"Movie Test\", artist=\"Matplotlib\", comment=\"a red circle following\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\/ex193.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2065","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=2065"}],"version-history":[{"count":4,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2065\/revisions"}],"predecessor-version":[{"id":2098,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2065\/revisions\/2098"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}