{"id":4567,"date":"2023-06-28T20:00:00","date_gmt":"2023-06-28T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=4567"},"modified":"2023-06-04T19:48:49","modified_gmt":"2023-06-04T10:48:49","slug":"matplotlib-save","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/matplotlib-save\/","title":{"rendered":"[matplotlib]How to Save a Plot to a File"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to save a plot using matplotlib<\/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 Save a Plot to a File in Matplotlib<\/p>\n\n\n\n<ul><li>How to Use &#8220;savefig&#8221; method<\/li><li>The Example Code to Save a Plot<\/li><\/ul>\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 Plot to a File in Matplotlib<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to save a plot to a file<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Matplotlib provides &#8220;matplotlib.pyplot.savefig&#8221; method for saving plot.<\/p>\n\n\n\n<p>First, I will show the use of the &#8220;savefig&#8221; method.<\/p>\n\n\n\n<p>Then, I will show the example code.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">How to Use &#8220;savefig&#8221; method<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to use &#8220;savefig&#8221; method<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The way to use of &#8220;savefig&#8221; method is described in <a href=\"https:\/\/matplotlib.org\/3.5.0\/api\/_as_gen\/matplotlib.pyplot.savefig.html\" target=\"_blank\" rel=\"noreferrer noopener\">matplotlib documentation<\/a>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center\"><p><img decoding=\"async\" width=\"967\" height=\"279\" class=\"wp-image-4572\" style=\"width: 600px;\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/06\/matplotlib_save.jpg\" alt=\"\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/06\/matplotlib_save.jpg 967w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/06\/matplotlib_save-300x87.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/06\/matplotlib_save-768x222.jpg 768w\" sizes=\"(max-width: 967px) 100vw, 967px\" \/><\/p><\/blockquote>\n\n\n\n<p>Specify a file path as an argument.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The image formats that can be saved are as follows.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eefaff;border-color:#1DA1F2;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#1DA1F2;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#fafafa;font-weight:bold;background-color:#1DA1F2;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Format<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>eps<\/li><li>jpeg<\/li><li>jpg<\/li><li>pdf<\/li><li>png<\/li><li>ps<\/li><li>raw<\/li><li>rgba<\/li><li>svg<\/li><li>svgz<\/li><li>tif<\/li><li>tiff<\/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<h3 class=\"wp-block-heading\">The Example Code to Save a Plot<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">the example code to save a plot<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import numpy as np\nimport matplotlib.pyplot as plt\nimport pathlib\n\n\nx1 = np.random.normal(0, 10, 1000)\nx2 = np.random.normal(20, 20, 1000)\n\n\nfig = plt.figure(figsize = (5,5), facecolor='lightblue')\n\nplt.xlabel('X')\nplt.ylabel('Y')\n\nplt.hist(x1,  bins=30, range=(-50, 100), ec='black', alpha = 0.5, label='x1')\nplt.hist(x2,  bins=30, range=(-50, 100), ec='black', alpha = 0.5, label='x2')\n\nplt.legend()\n\nplt.show()\n\n\npath_dir = pathlib.Path(r'test')\npath_img = path_dir.joinpath('img.png')\n\nfig.savefig(path_img)<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The above code generates the following graph.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"333\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_save_1-1.png\" alt=\"\" class=\"wp-image-4576\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_save_1-1.png 333w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_save_1-1-300x286.png 300w\" sizes=\"(max-width: 333px) 100vw, 333px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; x1 = np.random.normal(0, 10, 1000)<br>&gt; x2 = np.random.normal(20, 20, 1000)<\/p>\n\n\n\n<p>Random numbers are generated according to a normal distribution.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; fig = plt.figure(figsize = (5,5), facecolor=&#8217;lightblue&#8217;)<\/p>\n\n\n\n<p>The figure object is generated.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.xlabel(&#8216;X&#8217;)<br>&gt; plt.xlabel(&#8216;Y&#8217;)<\/p>\n\n\n\n<p>Labels are added.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.hist(x1, bins=30, range=(-50, 100), ec=&#8217;black&#8217;, alpha = 0.5, label=&#8217;x1&#8242;)<br>&gt; plt.hist(x2, bins=30, range=(-50, 100), ec=&#8217;black&#8217;, alpha = 0.5, label=&#8217;x2&#8242;)<\/p>\n\n\n\n<p>Specify data in list type as an argument to plot histograms.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.legend()<\/p>\n\n\n\n<p>Legend is added.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; path_dir = pathlib.Path(r&#8217;test&#8217;)<br>&gt; path_img = path_dir.joinpath(&#8216;img.png&#8217;)<\/p>\n\n\n\n<p>The path object is generated by using &#8220;pathlib&#8221; module.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; fig.savefig(path_img)<\/p>\n\n\n\n<p>The plot is saved as a image file by using &#8220;savefig&#8221; method.<\/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 save a plot using matplotlib. How to Save a Plot to a File in Matplotlib I &#8230; <\/p>\n","protected":false},"author":1,"featured_media":5551,"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\/4567"}],"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=4567"}],"version-history":[{"count":61,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/4567\/revisions"}],"predecessor-version":[{"id":5552,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/4567\/revisions\/5552"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/5551"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=4567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=4567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=4567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}