{"id":2094,"date":"2024-01-14T19:08:23","date_gmt":"2024-01-15T03:08:23","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2094"},"modified":"2024-01-14T19:08:23","modified_gmt":"2024-01-15T03:08:23","slug":"409-rain-drops-animation-using-matplotlib-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/409-rain-drops-animation-using-matplotlib-in-python\/","title":{"rendered":"#409 Rain drops &#8211; animation using matplotlib in python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/409-rain-drops-animation-using-matplotlib-in-python\/ex409\/\" rel=\"attachment wp-att-2095\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=700%2C700&#038;ssl=1\" alt=\"\" width=\"700\" height=\"700\" class=\"alignnone size-full wp-image-2095\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nfrom matplotlib.animation import FuncAnimation\r\n\r\n# Fixing random state for reproducibility\r\nnp.random.seed(19680801)\r\n\r\n\r\n# Create new Figure and an Axes which fills it.\r\nfig = plt.figure(figsize=(7, 7))\r\nax = fig.add_axes(&#x5B;0, 0, 1, 1], frameon=False)\r\nax.set_xlim(0, 1), ax.set_xticks(&#x5B;])\r\nax.set_ylim(0, 1), ax.set_yticks(&#x5B;])\r\n\r\n# Create rain data\r\nn_drops = 50\r\nrain_drops = np.zeros(n_drops, dtype=&#x5B;('position', float, (2,)),\r\n                                      ('size',     float),\r\n                                      ('growth',   float),\r\n                                      ('color',    float, (4,))])\r\n\r\n# Initialize the raindrops in random positions and with\r\n# random growth rates.\r\nrain_drops&#x5B;'position'] = np.random.uniform(0, 1, (n_drops, 2))\r\nrain_drops&#x5B;'growth'] = np.random.uniform(50, 200, n_drops)\r\n\r\n# Construct the scatter which we will update during animation\r\n# as the raindrops develop.\r\nscat = ax.scatter(rain_drops&#x5B;'position']&#x5B;:, 0], rain_drops&#x5B;'position']&#x5B;:, 1],\r\n                  s=rain_drops&#x5B;'size'], lw=0.5, edgecolors=rain_drops&#x5B;'color'],\r\n                  facecolors='none')\r\n\r\n\r\ndef update(frame_number):\r\n    # Get an index which we can use to re-spawn the oldest raindrop.\r\n    current_index = frame_number % n_drops\r\n\r\n    # Make all colors more transparent as time progresses.\r\n    rain_drops&#x5B;'color']&#x5B;:, 3] -= 1.0\/len(rain_drops)\r\n    rain_drops&#x5B;'color']&#x5B;:, 3] = np.clip(rain_drops&#x5B;'color']&#x5B;:, 3], 0, 1)\r\n\r\n    # Make all circles bigger.\r\n    rain_drops&#x5B;'size'] += rain_drops&#x5B;'growth']\r\n\r\n    # Pick a new position for oldest rain drop, resetting its size,\r\n    # color and growth factor.\r\n    rain_drops&#x5B;'position']&#x5B;current_index] = np.random.uniform(0, 1, 2)\r\n    rain_drops&#x5B;'size']&#x5B;current_index] = 5\r\n    rain_drops&#x5B;'color']&#x5B;current_index] = (0, 0, 0, 1)\r\n    rain_drops&#x5B;'growth']&#x5B;current_index] = np.random.uniform(50, 200)\r\n\r\n    # Update the scatter collection, with the new colors, sizes and positions.\r\n    scat.set_edgecolors(rain_drops&#x5B;'color'])\r\n    scat.set_sizes(rain_drops&#x5B;'size'])\r\n    scat.set_offsets(rain_drops&#x5B;'position'])\r\n\r\n\r\n# Construct the animation, using the update function as the animation director.\r\nanimation = FuncAnimation(fig, update, interval=10, save_count=100)\r\nanimation.save('ex409.gif', writer='imagemagick')\r\nplt.show()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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(&#x5B;0, 0, 1, 1], frameon=False) ax.set_xlim(0, 1), ax.set_xticks(&#x5B;]) ax.set_ylim(0, 1), ax.set_yticks(&#x5B;]) # Create rain data n_drops = 50 rain_drops = [&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_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-2094","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-xM","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":2094,"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":1088,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/188-create-animation-using-matplotlib\/","url_meta":{"origin":2094,"position":1},"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":1880,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/07\/356-animation-of-plot-legend-using-python\/","url_meta":{"origin":2094,"position":2},"title":"#356 Animation of plot legend using python","author":"gantovnik","date":"2023-07-01","format":false,"excerpt":"[code language=\"python\"] # pip install celluloid #%matplotlib qt from matplotlib import pyplot as plt from celluloid import Camera import numpy as np fig = plt.figure() camera = Camera(fig) t = np.linspace(0, 2 * np.pi, 128, endpoint=False) for i in range(0,41) : p=plt.plot(t, i*np.sin(t)) plt.legend(p, [f'line {i}']) camera.snap() animation = camera.animate()\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\/2023\/07\/ex356.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex356.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/07\/ex356.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1109,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/193-animation-using-python\/","url_meta":{"origin":2094,"position":3},"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":[]},{"id":1875,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/06\/354-animation-in-python\/","url_meta":{"origin":2094,"position":4},"title":"#354 Animation in python","author":"gantovnik","date":"2023-06-30","format":false,"excerpt":"[code language=\"python\"] # pip install celluloid #%matplotlib qt from matplotlib import pyplot as plt from celluloid import Camera import numpy as np fig, axes = plt.subplots(2) camera = Camera(fig) t = np.linspace(0, 2.0*np.pi, 128, endpoint=False) for i in t: axes[0].plot(t, np.sin(t + i), color='blue') axes[1].plot(t, np.sin(t - i), color='blue') camera.snap()\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\/2023\/06\/ex354.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.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":2094,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2094","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=2094"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2094\/revisions"}],"predecessor-version":[{"id":2097,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2094\/revisions\/2097"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}