{"id":8668,"date":"2020-09-28T12:29:34","date_gmt":"2020-09-28T12:29:34","guid":{"rendered":"https:\/\/www.askpython.com\/?p=8668"},"modified":"2020-09-28T12:29:36","modified_gmt":"2020-09-28T12:29:36","slug":"python-bar-plot","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-bar-plot","title":{"rendered":"Python Bar Plot &#8211; Visualize Categorical Data in Python"},"content":{"rendered":"\n<p>Hey, readers. In this article, we will be focusing on <strong>creating a Python bar plot<\/strong>.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.askpython.com\/python-modules\/data-visualization-using-python-bokeh\" class=\"rank-math-link\">Data visualization<\/a><\/strong> enables us to understand the data and helps us analyze the distribution of data in a pictorial manner.<\/p>\n\n\n\n<p><code>BarPlot<\/code> enables us to visualize the <strong>distribution of categorical data<\/strong> variables. They represent the distribution of discrete values. Thus, it represents the comparison of categorical values.<\/p>\n\n\n\n<p>The x axis represents the discrete values while the y axis represents the numeric values of comparison and vice versa.<\/p>\n\n\n\n<p>Let us now focus on the construction of Bar plots in the upcoming section.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Python Bar Plot Using Matplotlib<\/h2>\n\n\n\n<p><a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python-modules\/matplotlib\/python-matplotlib\">Python matplotlib module<\/a> provides us with various functions to plot the data and understand the distribution of the data values.<\/p>\n\n\n\n<p>The <code>matplotlib.pyplot.bar() function <\/code>is used to create a Bar plot using matplotlib module.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmatplotlib.pyplot.bar(x, height, width, bottom, align)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>x: The scalar x-coordinates of the barplot<\/li><li>height: The height of the bars to be plotted<\/li><li>bottom: The vertical baseline<\/li><li>width: The width of the bars to be plotted(optional)<\/li><li>align: The type of alignment of the bar plot(optional).<\/li><\/ul>\n\n\n\n<p>Further, we need to make sure and understand that only categorical data values can be provided to the barplot.<\/p>\n\n\n\n<p>Let us now try to implement a barplot using the matplotlib module.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\ncountry = &#x5B;&#039;INDIA&#039;, &#039;JAPAN&#039;, &#039;CHINA&#039;, &#039;USA&#039;, &#039;GERMANY&#039;]\npopulation = &#x5B;1000,800,600,400,1100]\nplt.bar(country,population)\nplt.show()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/BARPLOT-using-matplotlib.png\" alt=\"BARPLOT Using Matplotlib\" class=\"wp-image-8970\" width=\"535\" height=\"316\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/BARPLOT-using-matplotlib.png 535w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/BARPLOT-using-matplotlib-300x177.png 300w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><figcaption><strong>BARPLOT Using Matplotlib<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Bar Plot using Seaborn module<\/h2>\n\n\n\n<p><a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python-modules\/python-seaborn-tutorial\">Python Seaborn module<\/a> is built over the Matplotlib module and offers us with some advanced functionalities to have a better visualization of the data values.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nseaborn.barplot(x,y)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport seaborn as sn\nimport matplotlib.pyplot as plt\nimport pandas as pd\nBIKE = pd.read_csv(&quot;BIKE.csv&quot;)\nsn.barplot(x=&quot;season&quot;,y=&quot;cnt&quot;,data=BIKE)\nplt.show()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"568\" height=\"367\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/BARPLOT-using-Seaborn.png\" alt=\"BARPLOT Using Seaborn\" class=\"wp-image-8971\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/BARPLOT-using-Seaborn.png 568w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/BARPLOT-using-Seaborn-300x194.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><figcaption><strong>BARPLOT Using Seaborn<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the various techniques to construct a Python bar plot .<\/p>\n\n\n\n<p>Feel free to comment below, in case you come across any question.<\/p>\n\n\n\n<p>For more such posts related to Python, Stay tuned and till then, Happy Learning!!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/matplotlib.org\/3.1.1\/api\/_as_gen\/matplotlib.pyplot.bar.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Bar Plot with Python matplotlib module &#8212; Documentation<\/a> <\/li><li><a href=\"https:\/\/seaborn.pydata.org\/generated\/seaborn.barplot.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Bar Plot with Seaborn module &#8212; Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey, readers. In this article, we will be focusing on creating a Python bar plot. Data visualization enables us to understand the data and helps us analyze the distribution of data in a pictorial manner. BarPlot enables us to visualize the distribution of categorical data variables. They represent the distribution of discrete values. Thus, it [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":8973,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[95,1],"tags":[],"class_list":["post-8668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matplotlib","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=8668"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8668\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/8973"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=8668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=8668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=8668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}