{"id":3389,"date":"2023-02-27T20:00:00","date_gmt":"2023-02-27T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=3389"},"modified":"2023-02-26T21:56:33","modified_gmt":"2023-02-26T12:56:33","slug":"matplotlib-excel-paste","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/matplotlib-excel-paste\/","title":{"rendered":"[matplotlib]How to Insert Graphs into Excel"},"content":{"rendered":"\n<p>This article <span class=\"st-mymarker-s\">shows how to insert graphs into Excel.<\/span><\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p>\u25c6How to insert a graph created using matplotlib into Excel.<\/p>\n\n\n\n<ul><li>How to save a graph as an image and then insert it into Excel<\/li><li>[Supplement]How to keep a graph in PC memory and then insert it into Excel<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to save a graph as an image and then insert it into Excel<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to save a graph as an image and then insert it into Excel.<\/span><\/p>\n\n\n\n<p>By using &#8220;openpyxl&#8221;, a graph is inserted into Excel.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is the example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import matplotlib.pyplot as plt\nimport openpyxl\n\n\npath_fig = r'test\\test.png'\npath_excel = r'test\\test.xlsx'\n\n\n\n# Create graph\ndata_x = &#091;1,2,3,4,5]\ndata_y = &#091;x*x for x in data_x]\n\nfig, ax = plt.subplots(figsize = (5,5), facecolor='lightblue')\n\nax.plot(data_x, data_y, linestyle='solid', marker='o')\n\nax.set_xlabel('x')\nax.set_ylabel('y')\n\nplt.show()\nfig.savefig(path_fig)\n\n\n\n# Insert graph into excel\nwb = openpyxl.load_workbook(path_excel)\nsh = wb.create_sheet('graph')\n\nimg = openpyxl.drawing.image.Image(path_fig)\nsh.add_image(img, 'B2')\n\nwb.save(path_excel)\nwb.close()<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When the above code is executed, a graph is inserted into Excel.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"463\" height=\"413\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-excel-paste.jpg\" alt=\"\" class=\"wp-image-4066\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-excel-paste.jpg 463w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-excel-paste-300x268.jpg 300w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; fig.savefig(path_fig)<\/p>\n\n\n\n<p>Save the graph as an image using &#8220;savefig&#8221; method.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; wb = openpyxl.load_workbook(path_excel)<br>&gt; sh = wb.create_sheet(&#8216;graph&#8217;)<\/p>\n\n\n\n<p>An existing Excel file is opened and a new sheet is created.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; img = openpyxl.drawing.image.Image(fig_path)<br>&gt; sh.add_image(img, &#8216;B2&#8217;)<\/p>\n\n\n\n<p>By using &#8220;openpyxl&#8221;, the graph is loaded and pasted at &#8220;B2 cell&#8221;.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">[Supplement]How to keep a graph in PC memory and then insert it into Excel<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to keep a graph in PC memory and then insert it into Excel<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is the example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import matplotlib.pyplot as plt\nimport openpyxl\nimport io\n\n\npath_excel = r'test\\test.xlsx'\n\n\n# Create graph\ndata_x = &#091;1,2,3,4,5]\ndata_y = &#091;x*x for x in data_x]\n\nfig, ax = plt.subplots(figsize = (5,5), facecolor='lightblue')\n\nax.plot(data_x, data_y, linestyle='solid', marker='o')\n\nax.set_xlabel('x')\nax.set_ylabel('y')\n\nplt.show()\n\n\n\n# Insert graph into excel\nwb = openpyxl.load_workbook(path_excel)\nsh = wb.create_sheet('graph')\n\nimg_data = io.BytesIO()\nfig.savefig(img_data, format='png')\n\nimg = openpyxl.drawing.image.Image(img_data)\nsh.add_image(img, 'B2')\n\nwb.save(path_excel)\nwb.close()<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When the above code is executed, a graph is inserted into Excel.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"463\" height=\"413\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-excel-paste.jpg\" alt=\"\" class=\"wp-image-4066\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-excel-paste.jpg 463w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-excel-paste-300x268.jpg 300w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; img_data = io.BytesIO()<br>&gt; fig.savefig(img_data, format=&#8217;png&#8217;)<\/p>\n\n\n\n<p>&#8220;Io&#8221; module is a standard python module.<\/p>\n\n\n\n<p>By using &#8220;io&#8221; module, binary data can be handled in memory.<\/p>\n\n\n\n<p>By using &#8220;savefig&#8221; method, the graph data is kept in memory.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to insert graphs into Excel. How to save a graph as an image and then insert  &#8230; <\/p>\n","protected":false},"author":1,"featured_media":4116,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[20,21],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3389"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=3389"}],"version-history":[{"count":56,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3389\/revisions"}],"predecessor-version":[{"id":4115,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3389\/revisions\/4115"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/4116"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=3389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=3389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=3389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}