{"id":3392,"date":"2023-03-06T20:00:00","date_gmt":"2023-03-06T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=3392"},"modified":"2023-07-02T15:00:37","modified_gmt":"2023-07-02T06:00:37","slug":"matplotlib-legend","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/matplotlib-legend\/","title":{"rendered":"[matplotlib]How to Add Legend"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to add a legend<\/span> for a graph in matplotlib.<\/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><\/p>\n\n\n\n<ul><li>How to add legend<\/li><li>How to display the legend horizontally<\/li><li>How to change the legend position<\/li><li>How to place the legend outside of the plot<\/li><li>How to change legend font size<\/li><li>How to change legend font<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>First, I will show you how to add a legend as a basic introduction.<\/p>\n\n\n\n<p>After that, I will show you how to adjust the details of a legend, such as how to specify the position and change the font size.<\/p>\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 add legend<\/h2>\n\n\n\n<p>In the beginning, I show <span class=\"st-mymarker-s\">how to add legend<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The following two ways are explained below.<\/p>\n\n\n\n<ul><li>Using &#8220;label&#8221; in plot function<\/li><li>Using &#8220;legend function&#8221;<\/li><\/ul>\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\">Using &#8220;label&#8221; in plot function<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to add the legend using &#8220;label&#8221; in plot function<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the plot function, you can specify the legend string by using the &#8220;label&#8221; argument.<\/p>\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 numpy as np\nimport matplotlib.pyplot as plt\n\n\nx = np.linspace(0, 10, 100)\n\ny1 = np.sin(x)\ny2 = np.cos(x)\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(x, y1, linestyle='solid', marker='o', label='sin')\nax1.plot(x, y2, linestyle='solid', marker='o', label='cos')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend()\n\nplt.show()<\/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 is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend1.png\" alt=\"\" class=\"wp-image-4128\" width=\"300\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend1.png 400w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend1-300x278.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.plot(x, y1, linestyle=&#8217;solid&#8217;, marker=&#8217;o&#8217;, label=&#8217;sin&#8217;)<\/p>\n\n\n\n<p>label=&#8217;sin&#8217;   :    Specify a string to be displayed as a legend.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>ax1.legend( )<\/p>\n\n\n\n<p>Add the legend using legend function.<\/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\">Using &#8220;legend function&#8221;<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to add the legend using &#8220;legend function&#8221;<\/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 numpy as np\nimport matplotlib.pyplot as plt\n\n\nx = np.linspace(0, 10, 100)\n\ny1 = np.sin(x)\ny2 = np.cos(x)\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(x, y1, linestyle='solid', marker='o')\nax1.plot(x, y2, linestyle='solid', marker='o')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend(&#091;'sin', 'cos'])\n\nplt.show()<\/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. (The same graph in the section above.)<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend1.png\" alt=\"\" class=\"wp-image-4128\" width=\"300\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend1.png 400w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend1-300x278.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.legend([&#8216;sin&#8217;, &#8216;cos&#8217;])<\/p>\n\n\n\n<p>Specify a string to be displayed as a legend.<\/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\">How to display the legend horizontally<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to display the legend horizontally<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can specify the number of columns in the legend by using &#8220;ncol&#8221; argument.<\/p>\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 numpy as np\nimport matplotlib.pyplot as plt\n\n\nx = np.linspace(0, 10, 100)\n\ny1 = np.sin(x)\ny2 = np.cos(x)\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(x, y1, linestyle='solid', marker='o', label='sin')\nax1.plot(x, y2, linestyle='solid', marker='o', label='cos')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend(ncol=2)\n\nplt.show()\n<\/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 is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend2.png\" alt=\"\" class=\"wp-image-4135\" width=\"300\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend2.png 400w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend2-300x278.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.legend(ncol=2)<\/p>\n\n\n\n<p>To display the legend in two columns, &#8220;ncol=2&#8221; is specified.<\/p>\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 change the legend position<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to change the legend position<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The following three ways are explained below.<\/p>\n\n\n\n<ul><li>Using &#8220;loc=( x, y )&#8221;<\/li><li>Specified by &#8220;loc = location&#8221;<\/li><li>Specified by &#8220;loc&#8221; and &#8220;bbox_to_anchor&#8221;<\/li><\/ul>\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\">Using &#8220;loc=( x, y )&#8221;<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to change the legend position using &#8220;loc=( x, y )&#8221;.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can specify the legend position with the bottom left as (0,0).<\/p>\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\n\n\ndata_x = &#091;1,2,3,4,5]\ndata_y = &#091;x*x for x in data_x]\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(data_x, data_y, linestyle='solid', marker='o', label='squared')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend(loc=(0.1, 0.9))\n\nplt.show()<\/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\n<p>\u25c6 loc = (0.1, 0.9)<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend3.png\" alt=\"\" class=\"wp-image-4142\" width=\"287\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend3.png 382w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/03\/matplotlib-legend3-300x291.png 300w\" sizes=\"(max-width: 287px) 100vw, 287px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u25c6 loc = (0.6, 0.9)<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend4.png\" alt=\"\" class=\"wp-image-4143\" width=\"287\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend4.png 382w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend4-300x291.png 300w\" sizes=\"(max-width: 287px) 100vw, 287px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.legend(loc=(0.1, 0.9))<\/p>\n\n\n\n<p>As an argument of the legend function, &#8220;loc=(x,y)&#8221; is specified.<\/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\">Using &#8220;loc = location&#8221;<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to change the legend position using &#8220;loc = location&#8221;<\/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\n\nlocs = &#091;\n    'upper left',\n    'upper right',\n    'lower left',\n    'lower right',\n    'center left',\n    'center right',\n    'upper center',\n    'lower center',\n    'center',\n    'best',\n    'right',\n]\n\ndata_x = &#091;1,2,3,4,5]\ndata_y = &#091;x*x for x in data_x]\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(data_x, data_y, linestyle='solid', marker='o', label='squared')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend(loc=locs&#091;4])\n\nplt.show()<\/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 is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend5.png\" alt=\"\" class=\"wp-image-4145\" width=\"287\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend5.png 382w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend5-300x291.png 300w\" sizes=\"(max-width: 287px) 100vw, 287px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.legend(loc=locs[4])<\/p>\n\n\n\n<p>As an argument of the legend function, &#8220;loc = location&#8221; is specified.<\/p>\n\n\n\n<p>In above example code, location is &#8220;center left&#8221;.<\/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\">Using &#8220;loc&#8221; and &#8220;bbox_to_anchor&#8221;<\/h3>\n\n\n\n<p>I show how to change the legend position using &#8220;loc&#8221; and &#8220;bbox_to_anchor&#8221;.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can specify the legend position by specifying a range using &#8220;bbox_to_anchor&#8221; and a relative position using &#8220;loc&#8221;.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"486\" height=\"356\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_legend_bbox_to_anchor_loc.png\" alt=\"\" class=\"wp-image-4151\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_legend_bbox_to_anchor_loc.png 486w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_legend_bbox_to_anchor_loc-300x220.png 300w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/figure><\/div>\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Specify a range   :   &#8220;bbox_to_anchor = ( x, y, width, height )&#8221;<\/p>\n\n\n\n<p>Specify a relative position  :   &#8220;loc = location&#8221;<\/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\n\nlocs = &#091;\n    'upper left',\n    'upper right',\n    'lower left',\n    'lower right',\n    'center left',\n    'center right',\n    'upper center',\n    'lower center',\n    'center',\n    'best',\n    'right',\n]\n\ndata_x = &#091;1,2,3,4,5]\ndata_y = &#091;x*x for x in data_x]\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(data_x, data_y, linestyle='solid', marker='o', label='squared')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend(loc=locs&#091;0], bbox_to_anchor=(0, 0, 1, 1))\n\nplt.show()<\/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\n<p>\u25c6 bbox_to_anchor=(0, 0, 1, 1), loc = &#8220;upper left&#8221;<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend6.png\" alt=\"\" class=\"wp-image-4149\" width=\"287\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend6.png 382w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend6-300x291.png 300w\" sizes=\"(max-width: 287px) 100vw, 287px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u25c6 bbox_to_anchor=(0.5, 0, 0.5, 1), loc = &#8220;center left&#8221;<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend7.png\" alt=\"\" class=\"wp-image-4153\" width=\"287\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend7.png 382w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend7-300x291.png 300w\" sizes=\"(max-width: 287px) 100vw, 287px\" \/><\/figure><\/div>\n\n\n<div style=\"height:31px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.legend(loc=locs[0], bbox_to_anchor=(0, 0, 1, 1))<\/p>\n\n\n\n<p>As an argument of the legend function, &#8220;loc&#8221; and &#8220;bbox_to_anchor&#8221; are specified.<\/p>\n\n\n\n<p>In above example code, loc&#8221; is &#8220;upper left&#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<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">How to place the legend outside of the plot<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to place the legend outside of the plot<\/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\n\nlocs = &#091;\n    'upper left',\n    'upper right',\n    'lower left',\n    'lower right',\n    'center left',\n    'center right',\n    'upper center',\n    'lower center',\n    'center',\n    'best',\n    'right',\n]\n\ny1 = np.sin(x)\ny2 = np.cos(x)\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(x, y1, linestyle='solid', marker='o', label='sin')\nax1.plot(x, y2, linestyle='solid', marker='o', label='cos')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend(loc=locs&#091;0], bbox_to_anchor=(1.05, 1.0))\n\nplt.show()<\/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 is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend8.png\" alt=\"\" class=\"wp-image-4160\" width=\"356\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend8.png 475w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend8-300x234.png 300w\" sizes=\"(max-width: 356px) 100vw, 356px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.legend(loc=locs[0], bbox_to_anchor=(1.05, 1.0))<\/p>\n\n\n\n<p>&#8220;bbox_to_anchor=(1.05, 1.0)&#8221; is specified so that the legend is placed outside the plot.<\/p>\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 change legend font size<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to change legend font size.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can change legend font size by using &#8220;fontsize&#8221; argument.<\/p>\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\n\n\ny1 = np.sin(x)\ny2 = np.cos(x)\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(x, y1, linestyle='solid', marker='o', label='sin')\nax1.plot(x, y2, linestyle='solid', marker='o', label='cos')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend(fontsize=20)\n\nplt.show()<\/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 is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend9.png\" alt=\"\" class=\"wp-image-4172\" width=\"300\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend9.png 400w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend9-300x278.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.legend(fontsize=20)<\/p>\n\n\n\n<p>The legend font size is changed by passing fontsize=20 as an argument.<\/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\">How to change legend font<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to change legend font.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The fonts available in matplotlib are listed in the following files.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\">C:\/Users\/&#8221;User name&#8221;\/.matplotlib\/fontlist-v330.json<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"462\" height=\"370\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_font.jpg\" alt=\"\" class=\"wp-image-4174\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_font.jpg 462w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib_font-300x240.jpg 300w\" sizes=\"(max-width: 462px) 100vw, 462px\" \/><\/figure><\/div>\n\n\n<p>The font name is &#8220;name&#8221; in the above file.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can change legend font, by specifying a <strong>dictionary<\/strong> whose <strong>key is &#8220;familly&#8221;<\/strong> and whose <strong>value is &#8220;the font name&#8221;<\/strong> in the argument <strong>prop<\/strong>.<\/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\n\n\ny1 = np.sin(x)\ny2 = np.cos(x)\n\n\nfig = plt.figure(figsize = (6,6), facecolor='lightblue')\nax1 = fig.add_subplot(1, 1, 1)\n\nax1.plot(x, y1, linestyle='solid', marker='o', label='sin')\nax1.plot(x, y2, linestyle='solid', marker='o', label='cos')\n\nax1.set_xlabel('X')\nax1.set_ylabel('Y')\n\nax1.legend(prop={'family':'Courier New'})\n\nplt.show()<\/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 is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend10.png\" alt=\"\" class=\"wp-image-4170\" width=\"300\" height=\"278\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend10.png 400w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/02\/matplotlib-legend10-300x278.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.legend(prop={&#8216;family&#8217;:&#8217;Courier New&#8217;})<\/p>\n\n\n\n<p>The font is changed to Courier New.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The following article explains that the list of fonts available in matplotlib.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/matplotlib-font\/\"><a href=\"https:\/\/python-academia.com\/en\/wp-admin\/post.php?post=4953&amp;action=edit\">List of available Fonts<\/a><\/a><\/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 add a legend for a graph in matplotlib. First, I will show you how to add  &#8230; <\/p>\n","protected":false},"author":1,"featured_media":4314,"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\/3392"}],"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=3392"}],"version-history":[{"count":190,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3392\/revisions"}],"predecessor-version":[{"id":5879,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3392\/revisions\/5879"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/4314"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=3392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=3392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=3392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}