{"id":1089,"date":"2016-08-04T13:48:48","date_gmt":"2016-08-04T13:48:48","guid":{"rendered":"https:\/\/block.arch.ethz.ch\/blog\/?p=1089"},"modified":"2016-08-04T13:49:12","modified_gmt":"2016-08-04T13:49:12","slug":"dynamic-plotting-with-matplotlib","status":"publish","type":"post","link":"https:\/\/block.arch.ethz.ch\/blog\/2016\/08\/dynamic-plotting-with-matplotlib\/","title":{"rendered":"Dynamic plotting with matplotlib"},"content":{"rendered":"<p>Matplotlib is a great tool to visualise two-dimensional geometric data (and 3D data to some extent). You can also use it to dynamically visualise the convergence of an iterative solver.<\/p>\n<p><!--more--><\/p>\n<p>Surprisingly, you don&#8217;t need any fancy functionality to accomplish this, such as, for example, the <code>FuncAnimation<\/code> object of the <code>animation<\/code> package.<\/p>\n<pre class=\"brush:py;toolbars:false;gutter:false\">\r\nimport matplotlib.pyplot as plt\r\nimport time\r\nimport random\r\n\r\nysample = random.sample(xrange(-50, 50), 100)\r\n\r\nxdata = []\r\nydata = []\r\n\r\nplt.show()\r\n\r\naxes = plt.gca()\r\naxes.set_xlim(0, 100)\r\naxes.set_ylim(-50, +50)\r\nline, = axes.plot(xdata, ydata, 'r-')\r\n\r\nfor i in range(100):\r\n    xdata.append(i)\r\n    ydata.append(ysample[i])\r\n    line.set_xdata(xdata)\r\n    line.set_ydata(ydata)\r\n    plt.draw()\r\n    plt.pause(1e-17)\r\n    time.sleep(0.1)\r\n\r\n# add this if you don't want the window to disappear at the end \r\nplt.show()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Matplotlib is a great tool to visualise two-dimensional geometric data (and 3D data to some extent). You can also use it to dynamically visualise the convergence of an iterative solver.<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1089","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/comments?post=1089"}],"version-history":[{"count":1,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1089\/revisions"}],"predecessor-version":[{"id":1090,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1089\/revisions\/1090"}],"wp:attachment":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/media?parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/categories?post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/tags?post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}