{"id":1694,"date":"2015-05-21T23:17:17","date_gmt":"2015-05-21T22:17:17","guid":{"rendered":"http:\/\/www.2diabolos.com\/blog\/?page_id=1694"},"modified":"2024-08-26T19:46:29","modified_gmt":"2024-08-26T18:46:29","slug":"optimization-line-plotting","status":"publish","type":"page","link":"https:\/\/www.2diabolos.com\/blog\/python-and-spyder\/optimization-line-plotting\/","title":{"rendered":"Optimization line plotting"},"content":{"rendered":"<div class=\"columns\"><p>Usually, in order to optimize (accelerate) your code, you must vectorize as much as possible, and avoid for loops.<\/p>\n<p>Numpy does that great and allow you to manipulate matrix without the need of a for loop.<\/p>\n<p>But sometimes, I have been surprised. I wanted to draw a \u00ab\u00a0thick\u00a0\u00bb line in a graph. I needed to have all the coordinates, and I thought that directly plotting them was a good idea to see if I had an error.<\/p>\n<p>So on the code below, you may see an example of vectorized generation of 2D matrix (the xx and yy) : I did not use any loop, just adding \u00ab\u00a0vertical\u00a0\u00bb matrix (generated with numpy.vstack) and \u00ab\u00a0horizontal\u00a0\u00bb matrix.<br \/>\nand then I show 3 syntax to plot them, producing exactly the same graph . I was really surprised by the 2 first. The for loop is here more that 300 times faster than plotting directly \u00ab\u00a0xx, yy\u00a0\u00bb. I really had existential questions like \u00ab\u00a0do I understand really vectorization ?\u00a0\u00bb \u00ab\u00a0am I completely stupid or matplotlib is weird programmed?\u00a0\u00bb.<\/p>\n<p>I finally found the right way to do \u00ab\u00a0vectorized plotting\u00a0\u00bb : transpose the matrix to have long dimension (here the length) as the first index. And actually, if I did not specified the color of my line (with plot argument &lsquo;r-&lsquo;) I would have directly seen what&rsquo;s was going on : I plotted 20000 lines with a length of 20&#8230; instead of ploting 20 long lines!<\/p>\n<p>So you have seen the difference between the 2nd and the 3rd way to plot : the \u00ab\u00a0T\u00a0\u00bb which mean \u00ab\u00a0transpose\u00a0\u00bb in numpy, and accelerate the plot time by a factor bigger than 300 and is even 10% better than the for loop (first method). So now everything is reasonable again : vectorialization is faster than for loop \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p>[pastacode lang=\u00a0\u00bbpython\u00a0\u00bb path_id=\u00a0\u00bb\/2015\/05\/optimization_Line_drawing.py\u00a0\u00bb highlight=\u00a0\u00bb\u00a0\u00bb lines=\u00a0\u00bb\u00a0\u00bb provider=\u00a0\u00bbfile\u00a0\u00bb\/]<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Usually, in order to optimize (accelerate) your code, you must vectorize as much as possible, and avoid for loops. Numpy does that great and allow you to manipulate matrix without the need of a for loop. But sometimes, I have been surprised. I wanted to draw a \u00ab\u00a0thick\u00a0\u00bb line in a graph. I needed to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":861,"menu_order":150,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-1694","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/comments?post=1694"}],"version-history":[{"count":7,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1694\/revisions"}],"predecessor-version":[{"id":1710,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1694\/revisions\/1710"}],"up":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/861"}],"wp:attachment":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/media?parent=1694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}