{"id":4417,"date":"2023-06-21T20:00:00","date_gmt":"2023-06-21T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=4417"},"modified":"2023-06-04T16:45:22","modified_gmt":"2023-06-04T07:45:22","slug":"matplotlib-bargraph","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/matplotlib-bargraph\/","title":{"rendered":"[matplotlib]How to Create a Bar Plot in Python"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to create a bar plot 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><li>How to Create a Bar Plot<\/li><li>How to Set Bar Width<\/li><li>How to Set Bar Color<\/li><li>How to Add Edge of Bar<\/li><li>How to Plot Multiple Bar Plots<\/li><li>How to Create a Stacked Bar Plot<\/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 create a bar plot<\/h2>\n\n\n\n<p>In  the beginning, I show <span class=\"st-mymarker-s\">how to create a bar plot.<\/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 bar plot.<\/p>\n\n\n\n<p>The way to use of &#8220;matplotlib.pyplot.bar&#8221; is described in <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.pyplot.bar.html\">matplotlib documentation<\/a>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center\"><p><img decoding=\"async\" width=\"988\" height=\"157\" class=\"wp-image-4426\" style=\"width: 600px;\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/06\/matplotlib_bargraph_1-1.jpg\" alt=\"\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/06\/matplotlib_bargraph_1-1.jpg 988w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/06\/matplotlib_bargraph_1-1-300x48.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/06\/matplotlib_bargraph_1-1-768x122.jpg 768w\" sizes=\"(max-width: 988px) 100vw, 988px\" \/><\/p><cite><a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.pyplot.bar.html\" target=\"_blank\" rel=\"noreferrer noopener\">matplotlib documentation<\/a><\/cite><\/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 bar plot using &#8220;matplotlib.pyplot.bar&#8221;.<\/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 \nx = np.array(&#091;1, 2, 3, 4, 5])\ny = np.array(&#091;10, 20, 30, 40, 50])\n\n\nplt.bar(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=\"368\" height=\"248\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_1-2.png\" alt=\"\" class=\"wp-image-4433\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_1-2.png 368w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_1-2-300x202.png 300w\" sizes=\"(max-width: 368px) 100vw, 368px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.bar(x, y)<\/p>\n\n\n\n<p>The variables &#8220;x&#8221; and &#8220;y&#8221; are data lists for bar plot 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\">Example of adding graph elements to a bar plot<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">example of adding axis labels, legends etc. to a bar 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 numpy as np\nimport matplotlib.pyplot as plt\n \nx = np.array(&#091;1, 2, 3, 4, 5])\ny = np.array(&#091;10, 20, 30, 40, 50])\n\n\nfig = plt.figure(figsize = (5,5), facecolor='lightblue')\n\nplt.xlabel('X')\nplt.ylabel('Y')\n\nplt.bar(x, y, label='bar')\n\nplt.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=\"327\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_1-1.png\" alt=\"\" class=\"wp-image-4432\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_1-1.png 327w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_1-1-300x291.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" 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>A figure object 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; plt.xlabel(&#8216;X&#8217;)<br>&gt; plt.ylabel(&#8216;Y&#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; plt.bar(x, y, label=&#8217;bar&#8217;)<\/p>\n\n\n\n<p>The &#8220;label=&#8221; argument is used to specify the legend string.<\/p>\n\n\n\n<p>The following article explains how to add legend.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/matplotlib-legend\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Add Legend<\/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 Set Bar Width<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to set bar width<\/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 bar width by specifying &#8220;width=&#8221; as an 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 \nx = np.array(&#091;1, 2, 3, 4, 5])\ny = np.array(&#091;10, 20, 30, 40, 50])\n\n\nfig = plt.figure(figsize = (5,5), facecolor='lightblue')\n\nplt.xlabel('X')\nplt.ylabel('Y')\n\nplt.bar(x, y, label='bar', width=0.1)\n\nplt.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\n<p>\u25c6width=0.1<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"327\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_2-1.png\" alt=\"\" class=\"wp-image-4445\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_2-1.png 327w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_2-1-300x291.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u25c6width=1<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"327\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_2-2.png\" alt=\"\" class=\"wp-image-4447\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_2-2.png 327w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_2-2-300x291.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.bar(x, y, label=&#8217;bar&#8217;, width=0.1)<\/p>\n\n\n\n<p>The bar width is changed by specifying the argument &#8220;width=&#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 Set Bar Color<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to set bar color<\/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 bar color by specifying &#8220;color=&#8221; as an 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 \nx = np.array(&#091;1, 2, 3, 4, 5])\ny = np.array(&#091;10, 20, 30, 40, 50])\n\n\nfig = plt.figure(figsize = (5,5), facecolor='lightblue')\n\nplt.xlabel('X')\nplt.ylabel('Y')\n\nplt.bar(x, y, label='bar', width=0.8, color='GreenYellow')\n\nplt.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=\"327\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_3-1.png\" alt=\"\" class=\"wp-image-4453\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_3-1.png 327w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_3-1-300x291.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.bar(x, y, label=&#8217;bar&#8217;, width=0.8, color=&#8217;GreenYellow&#8217;)<\/p>\n\n\n\n<p>The bar color is changed by specifying the argument &#8220;color=&#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 Add Edge of Bar<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to add edge of bar.<\/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 edge of bar by specifying &#8220;edgecolor=&#8221; as an 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 \nx = np.array(&#091;1, 2, 3, 4, 5])\ny = np.array(&#091;10, 20, 30, 40, 50])\n\n\nfig = plt.figure(figsize = (5,5), facecolor='lightblue')\n\nplt.xlabel('X')\nplt.ylabel('Y')\n\nplt.bar(x, y, label='bar', width=0.8, color='GreenYellow', edgecolor='Green')\n\nplt.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=\"327\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_4-1.png\" alt=\"\" class=\"wp-image-4457\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_4-1.png 327w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_4-1-300x291.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.bar(x, y, label=&#8217;bar&#8217;, width=0.8, color=&#8217;GreenYellow&#8217;, edgecolor=&#8217;Green&#8217;)<\/p>\n\n\n\n<p>The color of bar edge is changed by specifying the argument &#8220;edgecolor=&#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\">How to Set Edge Width of Bar Plot<\/h3>\n\n\n\n<p>I show how to set edge width of bar plot.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can set edge width by specifying &#8220;linewidth=&#8221; as an 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 \nx = np.array(&#091;1, 2, 3, 4, 5])\ny = np.array(&#091;10, 20, 30, 40, 50])\n\n\nfig = plt.figure(figsize = (5,5), facecolor='lightblue')\n\nplt.xlabel('X')\nplt.ylabel('Y')\n\nplt.bar(x, y, label='bar', width=0.8, color='GreenYellow', edgecolor='Green', linewidth=5)\n\nplt.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=\"327\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_4-2.png\" alt=\"\" class=\"wp-image-4462\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_4-2.png 327w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_4-2-300x291.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.bar(x, y, label=&#8217;bar&#8217;, width=0.8, color=&#8217;GreenYellow&#8217;, edgecolor=&#8217;Green&#8217;, linewidth=5)<\/p>\n\n\n\n<p>The edge width is changed by specifying the argument &#8220;linewidth=&#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\">How to Plot Multiple Bar Plots<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to plot multiple bar plots<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>To plot the two bars side by side, the x-axis elements are shifted by half its width.<\/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\nwidth_bar = 0.4\n\nx = np.array(&#091;1, 2, 3, 4, 5])\nx1 = x - width_bar\/2\nx2 = x + width_bar\/2\n\ny1 = np.array(&#091;10, 20, 30, 40, 50])\ny2 = np.array(&#091;10, 15, 10, 15, 10])\n\n\nfig = plt.figure(figsize = (5,5), facecolor='lightblue')\n\nplt.xlabel('X')\nplt.ylabel('Y')\n\nplt.bar(x1, y1, label='y1', width=width_bar, align=\"center\")\nplt.bar(x2, y2, label='y2', width=width_bar, align=\"center\")\n\nplt.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=\"327\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_5-1.png\" alt=\"\" class=\"wp-image-4468\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_5-1.png 327w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_5-1-300x291.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; x = np.array([1, 2, 3, 4, 5])<br>&gt; x1 = x &#8211; width_bar\/2<br>&gt; x2 = x + width_bar\/2<\/p>\n\n\n\n<p>The x-axis elements are shifted by half its width.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.bar(x1, y1, label=&#8217;y1&#8242;, width=width_bar, align=&#8221;center&#8221;)<br>&gt; plt.bar(x2, y2, label=&#8217;y2&#8242;, width=width_bar, align=&#8221;center&#8221;)<\/p>\n\n\n\n<p>Two bar plots are plotted.<\/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 Create a Stacked Bar Plot<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to create a stacked bar plot<\/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 create a stacked bar plot by specifying &#8220;bottom=&#8221; as an 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\nwidth_bar = 0.8\n\nx = np.array(&#091;1, 2, 3, 4, 5])\n\ny1 = np.array(&#091;10, 20, 30, 40, 50])\ny2 = np.array(&#091;10, 15, 10, 15, 10])\n\n\nfig = plt.figure(figsize = (5,5), facecolor='lightblue')\n\nplt.xlabel('X')\nplt.ylabel('Y')\n\nplt.bar(x, y1, label='y1', width=width_bar, align=\"center\")\nplt.bar(x, y2, label='y2', width=width_bar, align=\"center\",bottom=y1)\n\nplt.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=\"327\" height=\"317\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_6-1.png\" alt=\"\" class=\"wp-image-4474\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_6-1.png 327w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2023\/05\/matplotlib_bargraph_6-1-300x291.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; plt.bar(x, y1, label=&#8217;y1&#8242;, width=width_bar, align=&#8221;center&#8221;)<br>&gt; plt.bar(x, y2, label=&#8217;y2&#8242;, width=width_bar, align=&#8221;center&#8221;,bottom=y1<\/p>\n\n\n\n<p>By specifying &#8220;bottom=y1&#8221;, The bar &#8220;y2&#8221; can be stacked on top of the bar &#8220;y1&#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<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 create a bar plot in matplotlib. How to create a bar plot In the beginning &#8230; <\/p>\n","protected":false},"author":1,"featured_media":5408,"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\/4417"}],"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=4417"}],"version-history":[{"count":143,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/4417\/revisions"}],"predecessor-version":[{"id":5504,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/4417\/revisions\/5504"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/5408"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=4417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=4417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=4417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}