{"id":1211,"date":"2021-11-28T01:45:02","date_gmt":"2021-11-28T09:45:02","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1211"},"modified":"2021-11-28T01:45:02","modified_gmt":"2021-11-28T09:45:02","slug":"210-parametric-curve-in-3d-2-2-2-2","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2\/","title":{"rendered":"#214 Animated 3D random walk"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib.animation as animation\r\n# Fixing random state for reproducibility\r\nnp.random.seed(19680801)\r\n\r\ndef random_walk(num_steps, max_step=0.05):\r\n    &quot;&quot;&quot;Return a 3D random walk as (num_steps, 3) array.&quot;&quot;&quot;\r\n    start_pos = np.random.random(3)\r\n    steps = np.random.uniform(-max_step, max_step, size=(num_steps, 3))\r\n    walk = start_pos + np.cumsum(steps, axis=0)\r\n    return walk\r\n\r\ndef update_lines(num, walks, lines):\r\n    for line, walk in zip(lines, walks):\r\n        # NOTE: there is no .set_data() for 3 dim data...\r\n        line.set_data(walk&#x5B;:num, :2].T)\r\n        line.set_3d_properties(walk&#x5B;:num, 2])\r\n    return lines\r\n\r\n# Data: 20 random walks as (num_steps, 3) arrays\r\nnum_steps = 100\r\nwalks = &#x5B;random_walk(num_steps) for index in range(20)]\r\n# Attaching 3D axis to the figure\r\nfig = plt.figure()\r\nax = fig.add_subplot(projection=&quot;3d&quot;)\r\n# Create lines initially without data\r\nlines = &#x5B;ax.plot(&#x5B;], &#x5B;], &#x5B;],lw=0.5)&#x5B;0] for _ in walks]\r\n# Setting the axes properties\r\nax.set(xlim3d=(0, 1), xlabel='X')\r\nax.set(ylim3d=(0, 1), ylabel='Y')\r\nax.set(zlim3d=(0, 1), zlabel='Z')\r\n# Creating the Animation object\r\nani = animation.FuncAnimation(fig, update_lines, num_steps, fargs=(walks, lines), interval=100,repeat=False)\r\nplt.savefig('ex214.png', dpi=72)\r\nplt.show()\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex214.png?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-1212\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex214.png 640w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex214-480x360.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 640px, 100vw\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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): &quot;&quot;&quot;Return a 3D random walk as (num_steps, 3) array.&quot;&quot;&quot; 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 def update_lines(num, walks, lines): for line, walk [&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-1211","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-jx","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":1211,"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":1211,"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":1188,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/204-mandelbrot-fractal-using-python-2-2-2-2\/","url_meta":{"origin":1211,"position":2},"title":"#208 Scatter plot with histograms using python","author":"gantovnik","date":"2021-11-27","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt Fixing random state for reproducibility np.random.seed(19680801) some random data x = np.random.randn(1000) y = np.random.randn(1000) def scatter_hist(x, y, ax, ax_histx, ax_histy): # no labels ax_histx.tick_params(axis=\"x\", labelbottom=False) ax_histy.tick_params(axis=\"y\", labelleft=False) # the scatter plot: ax. Scatter(x, y,s=3) # now determine nice limits\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\/ex208.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex208.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex208.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":993,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/177-random-snowflakes-using-python-turtle\/","url_meta":{"origin":1211,"position":3},"title":"#177 Random snowflakes using python turtle","author":"gantovnik","date":"2021-10-06","format":false,"excerpt":"#177 Random snowflakes using python turtle [code language=\"python\"] import turtle from random import randint def generate_random_colour(): # Generates an R,G,B values randomly in range 0 to 255 r = randint(0, 255) g = randint(0, 255) b = randint(0, 255) return r, g, b def setup_screen(title, background = 'white'): print('Set up\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\/10\/ex177.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":894,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/160-max-and-min-using-argmax-and-argmax-in-python\/","url_meta":{"origin":1211,"position":4},"title":"#160 Max and min using argmax and argmax in python","author":"gantovnik","date":"2021-02-14","format":false,"excerpt":"#160 Max and min using argmax and argmax in python [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import os os.chdir(r'D:\\projects\\wordpress\\ex160') os.getcwd() def main(): N = 100 L = 1 def f(i, n): x = i * L \/ N lam = 2 * L \/ (n+1) return\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\/02\/example160.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example160.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example160.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1201,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2\/","url_meta":{"origin":1211,"position":5},"title":"#211 Lorenz attractor","author":"gantovnik","date":"2021-11-27","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt def lorenz(x, y, z, s=10, r=28, b=2.667): \"\"\" Given: x, y, z: a point of interest in three dimensional space s, r, b: parameters defining the lorenz attractor Returns: x_dot, y_dot, z_dot: values of the lorenz attractor's partial derivatives at\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\/ex211.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1211","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=1211"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}