{"id":528,"date":"2024-03-12T09:31:56","date_gmt":"2024-03-12T12:31:56","guid":{"rendered":"https:\/\/utfpr.curitiba.br\/lassip\/?p=528"},"modified":"2024-03-12T09:31:58","modified_gmt":"2024-03-12T12:31:58","slug":"animated-plots-with-matplotlib","status":"publish","type":"post","link":"https:\/\/utfpr.curitiba.br\/lassip\/2024\/03\/12\/animated-plots-with-matplotlib\/","title":{"rendered":"Animated plots with Matplotlib"},"content":{"rendered":"\n<p>The script below will do this:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Animated plots with Matplotlib\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/7WBsIbzMA6A?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Enjoy it!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport time\r\n\r\n# N: number of elements in the plot\r\nN = 100\r\nx = np.arange(N)\r\n# Start with an all-zero vector (you can change this)\r\ny1 = np.zeros(N, dtype=float)\r\ny2 = np.zeros(N, dtype=float)\r\n\r\nfig, ax = plt.subplots()\r\nline1, = ax.plot(x, y1)\r\nline2, = ax.plot(x, y2)\r\nplt.grid()\r\nplt.legend(&#091;'Plot 1', 'Plot 2'], loc=2)\r\nplt.xticks(&#091;0, N\/\/2, N], &#091;'t-'+str(N), 't-'+str(N\/\/2), 't'])\r\nymin = 0\r\nymax = 0\r\n\r\nwhile True:\r\n    # Shift all elements to the left\r\n    y1 = np.roll(y1, -1)\r\n    y2 = np.roll(y2, -1)\r\n\r\n    # Insert new value at last element\r\n    y1&#091;-1] = np.cos(time.time()) + np.random.randn(1)*1e-1\r\n    y2&#091;-1] = np.sin(time.time()) + np.random.randn(1)*1e-1\r\n    # Update plot\r\n    line1.set_ydata(y1)\r\n    line2.set_ydata(y2)\r\n\r\n    # Adjust the range exhibited in the plot\r\n    if y1.min() &lt; ymin:\r\n        ymin = y1.min()\r\n    if y1.max() &gt; ymax:\r\n        ymax = y1.max()\r\n    if y2.min() &lt; ymin:\r\n        ymin = y2.min()\r\n    if y2.max() &gt; ymax:\r\n        ymax = y2.max()\r\n    ax.axis(&#091;0, N, ymin, ymax])\r\n\r\n    # Very small pause (required for update)\r\n    plt.pause(1e-2)\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The script below will do this: Enjoy it!<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[1],"tags":[],"class_list":["post-528","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/posts\/528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/comments?post=528"}],"version-history":[{"count":1,"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/posts\/528\/revisions"}],"predecessor-version":[{"id":529,"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/posts\/528\/revisions\/529"}],"wp:attachment":[{"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/media?parent=528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/categories?post=528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/utfpr.curitiba.br\/lassip\/wp-json\/wp\/v2\/tags?post=528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}