{"id":4988,"date":"2023-08-16T20:00:00","date_gmt":"2023-08-16T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=4988"},"modified":"2023-08-15T23:09:27","modified_gmt":"2023-08-15T14:09:27","slug":"matplotlib-linegarph","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/matplotlib-linegarph\/","title":{"rendered":"[matplotlib]How to Plot a Line Chart[marker, error bars]"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to plot a line chart in 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><\/p>\n\n\n\n<ul>\n<li>How to plot a line chart<\/li>\n\n\n\n<li>How to add markers to a line chart<\/li>\n\n\n\n<li>How to add values to a line chart<\/li>\n\n\n\n<li>How to add error bars to a line chart<\/li>\n\n\n\n<li>How to plot multiple line charts<\/li>\n\n\n\n<li>How to plot a line chart by using pandas<\/li>\n<\/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 plot a line chart<\/h2>\n\n\n\n<p>In the beginning, <span class=\"st-mymarker-s\">I show how to plot a line chart.<\/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 methods for drawing a line chart.<\/p>\n\n\n\n<p>The way to use of &#8220;matplotlib.pyplot.plot&#8221; is described in <a href=\"https:\/\/matplotlib.org\/3.5.0\/api\/_as_gen\/matplotlib.pyplot.scatter.html\">matplotlib documen<\/a><a href=\"https:\/\/matplotlib.org\/3.5.0\/api\/_as_gen\/matplotlib.pyplot.scatter.html\" target=\"_blank\" rel=\"noreferrer noopener\">t<\/a><a href=\"https:\/\/matplotlib.org\/3.5.0\/api\/_as_gen\/matplotlib.pyplot.scatter.html\">ation<\/a>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center\">\n<p><img decoding=\"async\" width=\"1034\" height=\"165\" class=\"wp-image-4895\" style=\"width: 600px;\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/07\/matplotlib-color-gradation_plot.jpg\" alt=\"\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/07\/matplotlib-color-gradation_plot.jpg 1034w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/07\/matplotlib-color-gradation_plot-300x48.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/07\/matplotlib-color-gradation_plot-1024x163.jpg 1024w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/07\/matplotlib-color-gradation_plot-768x123.jpg 768w\" sizes=\"(max-width: 1034px) 100vw, 1034px\" \/><\/p>\n\n\n\n<p><\/p>\n<\/blockquote>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is the example code to create a line chart using &#8220;matplotlib.pyplot.plot&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import matplotlib.pyplot as plt\nimport numpy as np\n\nx = list(range(1,6))\ny = np.random.rand(5)\n\nfig, ax = plt.subplots()\n\nax.plot(x, y)\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\"><img decoding=\"async\" width=\"372\" height=\"248\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_1-1.png\" alt=\"\" class=\"wp-image-4999\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_1-1.png 372w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_1-1-300x200.png 300w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.plot(x, y)<\/p>\n\n\n\n<p>The variables &#8220;x&#8221; and &#8220;y&#8221; are data lists for line chart generation.<\/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\">Add legends and labels to graph<\/h3>\n\n\n\n<p>I show an example of adding legends and labels to graph.<\/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 numpy as np\n\nx = list(range(1,6))\ny = np.random.rand(5)\n\nfig, ax = plt.subplots()\n\nax.plot(x, y, label = 'random')\n\nax.set_xlabel('X-label')\nax.set_ylabel('Y-label')\n\nax.set_title('Title')\n\nax.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\"><img decoding=\"async\" width=\"386\" height=\"278\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_1-2.png\" alt=\"\" class=\"wp-image-5004\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_1-2.png 386w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_1-2-300x216.png 300w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.plot(x, y, label = &#8216;random&#8217;)<\/p>\n\n\n\n<p>You can specify a string of legend by using &#8220;label=&#8221; argument.<\/p>\n\n\n\n<p>The following article explains how to add legend to graph.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/matplotlib-legend\/\">How to Add Legend<\/a><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.set_xlabel(&#8216;X-label&#8217;)<br>&gt; ax.set_ylabel(&#8216;Y-label&#8217;)<\/p>\n\n\n\n<p>Axis labels are added.<\/p>\n\n\n\n<p>The following article explains how to set axis elements.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/matplotlib-axis\/\">How to Set Axis Elements[Label, Range, Ticks]<\/a><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.set_title(&#8216;Title&#8217;)<\/p>\n\n\n\n<p>Title is added.<\/p>\n\n\n\n<p>The following article explains how to set axis elements.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/matplotlib-title\/\">How to Add a Title[Position, Size, Font]<\/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\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 add markers to a line chart<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to add markers to a line chart.<\/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 add markers to graph by using &#8220;marker=&#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\nimport numpy as np\n\nx = list(range(1,6))\ny = np.random.rand(5)\n\nfig, ax = plt.subplots()\n\nax.plot(x, y, marker='o')\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\"><img decoding=\"async\" width=\"372\" height=\"248\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_2-1.png\" alt=\"\" class=\"wp-image-5013\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_2-1.png 372w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_2-1-300x200.png 300w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.plot(x, y, marker=&#8217;o&#8217;)<\/p>\n\n\n\n<p>Circular markers are added.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you want to know more about marker types, please refer to the <a rel=\"noreferrer noopener\" href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.pyplot.plot.html\" target=\"_blank\">matplotlib documentation<\/a>.<\/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 values to a line chart<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to add values to a line chart<\/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 add values to graph by using &#8220;ax.text( )&#8221;.<\/p>\n\n\n\n<p>Specify &#8220;x-coordinate&#8221;, &#8220;y-coordinate&#8221;, and &#8220;value&#8221; as the arguments of ax.text( ).<\/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\nimport numpy as np\n\nx = list(range(1,6))\ny = np.round(np.random.rand(5), 2)\n\nfig, ax = plt.subplots()\n\nax.plot(x, y, marker='o')\n\nax.set_xlabel('X-label')\nax.set_ylabel('Y-label')\n\nfor i, value in enumerate(y):\n    ax.text(x&#091;i], y&#091;i], value)\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\"><img decoding=\"async\" width=\"393\" height=\"262\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_3-1.png\" alt=\"\" class=\"wp-image-5021\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_3-1.png 393w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_3-1-300x200.png 300w\" sizes=\"(max-width: 393px) 100vw, 393px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; for i, value in enumerate( y ):<br>&gt;     ax.text(x[ i ], y[ i ], value)<\/p>\n\n\n\n<p>Use enumerate function to get the index and element from a list.<\/p>\n\n\n\n<p>Specify &#8220;x-coordinate&#8221;, &#8220;y-coordinate&#8221;, and &#8220;value&#8221; are specified as the arguments.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The following article explains how to use enumerate function.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/enumerate\/\">How to use enumerate function<\/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<!-- 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 add error bars to a line chart<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to add error bars to a line chart.<\/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 add error bars to graph by using &#8220;ax.errorbar( )&#8221;.<\/p>\n\n\n\n<p>Specify &#8220;x-data&#8221;, &#8220;y-data&#8221;, and &#8220;error value&#8221; as the arguments of ax.errorbar( ).<\/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\nimport numpy as np\n\nx = list(range(1,6))\ny = np.round(np.random.rand(5), 2)\ne = y*0.1\n\nfig, ax = plt.subplots()\n\nax.plot(x, y, color='black')\nax.errorbar(x, y, yerr=e, marker='o', capthick=1, capsize=5, lw=1, color='black')\n\nax.set_xlabel('X-label')\nax.set_ylabel('Y-label')\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\"><img decoding=\"async\" width=\"386\" height=\"262\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_4-1.png\" alt=\"\" class=\"wp-image-5028\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_4-1.png 386w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_4-1-300x204.png 300w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; e = y*0.1<\/p>\n\n\n\n<p>The error value is assumed to be y multiplied by 0.1.<\/p>\n\n\n\n<p>The value &#8220;e&#8221; is list type.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.errorbar(x, y, yerr=e, marker=&#8217;o&#8217;, capthick=1, capsize=5, lw=1, color=&#8217;black&#8217;)<\/p>\n\n\n\n<p>Error bars are added.<\/p>\n\n\n\n<p>The error value &#8220;e&#8221; is specified by using &#8220;yerr=&#8221; argument.<\/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\">[Supplement]How to add error bars with different lengths at the top and bottom<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to add error bars with different lengths at the top and bottom<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Specify &#8220;lower error value &#8221; and &#8220;upper error value&#8221; by using arguments &#8220;yerr=&#8221;.<\/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\nimport numpy as np\n\nx = list(range(1,6))\ny = np.round(np.random.rand(5), 2)\ne_l = y*0.05\ne_u = y*0.1\n\nfig, ax = plt.subplots()\n\nax.plot(x, y, color='black')\nax.errorbar(x, y, yerr=&#091;e_l, e_u], marker='o', capthick=1, capsize=5, lw=1, color='black')\n\nax.set_xlabel('X-label')\nax.set_ylabel('Y-label')\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\"><img decoding=\"async\" width=\"386\" height=\"262\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_4-2.png\" alt=\"\" class=\"wp-image-5032\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_4-2.png 386w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_4-2-300x204.png 300w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; e_l = y*0.05<br>&gt; e_u = y*0.1<\/p>\n\n\n\n<p>Create two lists of error values.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.errorbar(x, y, yerr=[e_l, e_u], marker=&#8217;o&#8217;, capthick=1, capsize=5, lw=1, color=&#8217;black&#8217;)<\/p>\n\n\n\n<p>Specify &#8220;lower error value &#8221; and &#8220;upper error value&#8221; by using arguments &#8220;yerr=&#8221;.<\/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 plot multiple line charts<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to plot multiple line charts.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I show following two examples.<\/p>\n\n\n\n<ul>\n<li>How to plot multiple line charts on top of each other<\/li>\n\n\n\n<li>How to plot multiple line charts side by side<\/li>\n<\/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\">How to plot multiple line charts on top of each other<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to plot multiple line charts on top of each other.<\/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 numpy as np\n\nx = list(range(1,6))\ny1 = np.random.rand(5)\ny2 = np.random.rand(5)\n\nfig, ax = plt.subplots()\n\nax.plot(x, y1, marker='o', color='blue', label='y1')\nax.plot(x, y2, marker='o', color='red', label='y2')\n\nax.set_xlabel('X-label')\nax.set_ylabel('Y-label')\n\nax.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\"><img decoding=\"async\" width=\"386\" height=\"262\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_5-1.png\" alt=\"\" class=\"wp-image-5041\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_5-1.png 386w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_5-1-300x204.png 300w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax.plot(x, y1, marker=&#8217;o&#8217;, color=&#8217;blue&#8217;, label=&#8217;y1&#8242;)<br>&gt; ax.plot(x, y2, marker=&#8217;o&#8217;, color=&#8217;red&#8217;, label=&#8217;y2&#8242;)<\/p>\n\n\n\n<p>Two line charts are drawn in the same &#8220;subplot&#8221; area.<\/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 plot multiple line charts side by side<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to plot multiple line charts side by side.<\/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 numpy as np\n\nx = list(range(1,6))\ny1 = np.random.rand(5)\ny2 = np.random.rand(5)\n\n#\u30b0\u30e9\u30d5\u3092\u8868\u793a\u3059\u308b\u9818\u57df\u3092\uff0cfig\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u4f5c\u6210\uff0e\nfig = plt.figure(figsize = (10,6), facecolor='lightblue')\n\nax1 = fig.add_subplot(1, 2, 1)\nax2 = fig.add_subplot(1, 2, 2, sharey=ax1)\n\nax1.plot(x, y1, marker='o', color='blue', label='y1')\nax2.plot(x, y2, marker='o', color='red', label='y2')\n\nax1.set_xlabel('X-label')\nax1.set_ylabel('Y-label')\nax2.set_xlabel('X-label')\nax2.set_ylabel('Y-label')\n\nax1.legend() \nax2.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\"><img decoding=\"async\" width=\"609\" height=\"371\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_5-2.png\" alt=\"\" class=\"wp-image-5047\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_5-2.png 609w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_5-2-300x183.png 300w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1 = fig.add_subplot(1, 2, 1)<br>&gt; ax2 = fig.add_subplot(1, 2, 2, sharey=ax1)<\/p>\n\n\n\n<p>The subplots are added to the figure object.<\/p>\n\n\n\n<p>By setting &#8220;sharey=ax1&#8221;, the y-axis of ax2 is synchronized with that of ax1.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; ax1.plot(x, y1, marker=&#8217;o&#8217;, color=&#8217;blue&#8217;, label=&#8217;y1&#8242;)<br>&gt; ax2.plot(x, y2, marker=&#8217;o&#8217;, color=&#8217;red&#8217;, label=&#8217;y2&#8242;)<\/p>\n\n\n\n<p>The line charts are drawn for each subplot.<\/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 difference between &#8220;plt&#8221; plot and &#8220;ax&#8221; plot.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/matplotlib-plt-ax\/\">Difference between &#8220;plt&#8221; plot and &#8220;ax&#8221; plot<\/a><\/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 how to plot multiple graphs.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/matplotlib-multiplegraphs\/\">How to Plot Multiple Graphs<\/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\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 plot a line chart by using pandas<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to plot a line chart by using pandas<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Pandas is a data analysis support library in Python.<\/p>\n\n\n\n<p>Using Pandas methods, you can easily create a graph.<\/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 numpy as np\nimport pandas as pd\n\nx = list(range(1,6))\ny1 = np.random.rand(5)\ny2 = np.random.rand(5)\n\ndf = pd.DataFrame(data={'y1':y1,'y2':y2}, index=x)\n\ndf.plot(marker='o')<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>DataFrame contains data as shown here.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"182\" height=\"172\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_6-1.jpg\" alt=\"\" class=\"wp-image-5053\"\/><\/figure><\/div>\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=\"372\" height=\"248\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_6-2.png\" alt=\"\" class=\"wp-image-5057\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_6-2.png 372w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib-linegraph_6-2-300x200.png 300w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>> df = pd.DataFrame(data={&#8216;y1&#8242;:y1,&#8217;y2&#8217;:y2}, index=x)<\/p>\n\n\n\n<p>DataFrame is created.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>> df.plot(marker=&#8217;o&#8217;)<\/p>\n\n\n\n<p>Using &#8220;plot&#8221; method of DataFrame, a line chart is drawn.<\/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 plot a line chart in matplotlib. How to plot a line chart In the beginning &#8230; <\/p>\n","protected":false},"author":1,"featured_media":5985,"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\/4988"}],"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=4988"}],"version-history":[{"count":193,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/4988\/revisions"}],"predecessor-version":[{"id":6119,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/4988\/revisions\/6119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/5985"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=4988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=4988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=4988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}