{"id":968,"date":"2023-06-05T17:13:38","date_gmt":"2023-06-05T15:13:38","guid":{"rendered":"https:\/\/inseri.swiss\/?p=968"},"modified":"2024-11-11T17:13:38","modified_gmt":"2024-11-11T17:13:38","slug":"python-and-matplotlib","status":"publish","type":"post","link":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/2023\/06\/python-and-matplotlib\/","title":{"rendered":"Python and Matplotlib"},"content":{"rendered":"\n<p>In this example we will show you how to integrate our Python code for generating Matplotlib figures into the Python Code block and use the Image Box block to display it.<\/p>\n\n\n\n<p>It is important to keep in mind that the runtime is not CPython but rather Pyodide that behaves slightly different. For this use case please pay attention at the following points (see the code below):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>use the <kbd>Agg<\/kbd> backend which works fine with inseri-core (please see line 5)<\/li>\n\n\n\n<li>save the figure a a PNG file<\/li>\n<\/ul>\n\n\n\n<p>The PNG file is the <strong>output<\/strong> of our Python Code block, and used further as an input in the Image Box block.<\/p>\n\n\n\n<div class=\"wp-block-inseri-core-python\" data-attributes=\"{&quot;autoTrigger&quot;:false,&quot;blockId&quot;:&quot;9vuwaUz1sW9HjiTT83UfP&quot;,&quot;content&quot;:&quot;import%20matplotlib.pyplot%20as%20plt%0Aimport%20matplotlib%0A%0A%23%20matplotlib%20backend%20needed%20with%20inseri-core%0Amatplotlib.use('Agg')%0A%0Aplt.bar(x%3D%5B%22a%22%2C%20%22b%22%2C%20%22c%22%5D%2C%20height%3D%5B1%2C%203%2C%202%5D)%0Aplt.savefig(%22img.png%22%2C%20format%3D'png'%2C%20bbox_inches%3D'tight')%0Aplt.close()&quot;,&quot;editable&quot;:true,&quot;height&quot;:237,&quot;inputCode&quot;:&quot;&quot;,&quot;inputs&quot;:{},&quot;isVisible&quot;:true,&quot;label&quot;:&quot;Feel free to edit the Python code&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;python-matplotlib&quot;},&quot;mode&quot;:&quot;editor&quot;,&quot;outputs&quot;:[]}\">is loading &#8230;<\/div>\n\n\n\n<div class=\"wp-block-inseri-core-image\" data-attributes=\"{&quot;altText&quot;:&quot;&quot;,&quot;blockId&quot;:&quot;8tmcgej1x5nPW8CAP2BjA&quot;,&quot;caption&quot;:&quot;&quot;,&quot;fit&quot;:&quot;contain&quot;,&quot;height&quot;:null,&quot;inputKey&quot;:&quot;9vuwaUz1sW9HjiTT83UfP\/img.png&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;image&quot;}}\">is loading &#8230;<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:60%\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-inseri-core-export alignright\" data-attributes=\"{&quot;align&quot;:&quot;right&quot;,&quot;blockId&quot;:&quot;vaxQQCzmrnvQCf4meQgfk&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;export&quot;},&quot;postId&quot;:968}\">is loading &#8230;<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-inseri-core-share alignright\" data-attributes=\"{&quot;align&quot;:&quot;right&quot;,&quot;blockId&quot;:&quot;KTETwIissQyi0jL3ZpuwV&quot;,&quot;copiedText&quot;:&quot;Copied&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;share&quot;},&quot;showIcon&quot;:true,&quot;text&quot;:&quot;Share&quot;}\">is loading &#8230;<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we will show you how to integrate our Python code for generating Matplotlib figures into the Python Code block and use the Image Box block to display it. It is important to keep in mind that the runtime is not CPython but rather Pyodide that behaves slightly different. For this use case [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[40,43,48,50],"class_list":["post-968","post","type-post","status-publish","format-standard","hentry","category-basic-example","tag-export","tag-image-box","tag-python-code","tag-share"],"_links":{"self":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/comments?post=968"}],"version-history":[{"count":1,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/968\/revisions"}],"predecessor-version":[{"id":2126,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/968\/revisions\/2126"}],"wp:attachment":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/media?parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/categories?post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/tags?post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}