{"id":86936,"date":"2023-02-27T09:00:27","date_gmt":"2023-02-27T03:30:27","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=86936"},"modified":"2023-02-27T09:00:27","modified_gmt":"2023-02-27T03:30:27","slug":"how-to-plot-histogram-in-python","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/","title":{"rendered":"How to plot Histogram in Python?"},"content":{"rendered":"<p>In this tutorial, we will learn about histograms and how to plot Histogram in Python? Let&#8217;s start!!!<\/p>\n<h3>What is Histogram?<\/h3>\n<p><span style=\"font-weight: 400\">Histograms are a display of statistical information that uses rectangles to show the frequency of data items in successive numerical intervals of the same size.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Nearly everyone can use an intuitive understanding of a histogram to visualize and comprehend the probability distribution of numerical data or image data. Python has many different options for creating and plotting histograms.<\/span><\/p>\n<p><span style=\"font-weight: 400\">In Python, we can extensively use histograms in data visualization techniques using the &#8220;Matplotlib&#8221; library, a cross-platform data visualization and graphical plotting library for Python.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Types of Histogram:<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Depending on the frequency distribution of the data, there are numerous ways to divide a histogram. There are several different types of distributions, including the gaussian distribution, the bimodal distribution, the skewed distribution, the comb distribution, the edge peak distribution, the multimodal distribution, the dog food distribution, and others. These various types of distributions can be depicted using the histogram. The several types of histograms include:<\/span><\/p>\n<p><strong>1. Hexagonal Histogram: <\/strong><\/p>\n<p><span style=\"font-weight: 400\">An even distribution suggests there are too few groups. The number of entries in each group is the same. It might consist of a distribution with numerous peaks, all of which have the same heights.<\/span><\/p>\n<p><strong>2. Equilateral Histogram: <\/strong><\/p>\n<p><span style=\"font-weight: 400\">A symmetric histogram is sometimes known as a bell-shaped histogram. If a vertical line is drawn through the middle of the histogram, the opposite sides are said to be symmetrical if they are the same size and form.<\/span><\/p>\n<p><strong>3. The bimodal histogram: <\/strong><\/p>\n<p><span style=\"font-weight: 400\">If a histogram has two peaks, it is said to be bimodal. Bimodality is present when data collection includes observations on two separate types of individuals or combined groups, and the centers of the two unique histograms are sufficiently removed from the variance in both data sets.<\/span><\/p>\n<p><strong>4. Statistical Histogram: <\/strong><\/p>\n<p><span style=\"font-weight: 400\">This histogram is a visual representation of a discontinuous probability distribution. A rectangle represents every x-value. Each rectangle&#8217;s area directly relates to the probability that the related value will occur.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Calculating histograms in NumPy: Starting from the base:<\/span><\/h3>\n<p><span style=\"font-weight: 400\">You&#8217;ve been utilizing what is referred to as &#8220;frequency tables&#8221; up to this point. The mapping of bins (intervals) to frequencies, however, is what a histogram is theoretically. Technically speaking, it can be applied to approximate the probability density function (PDF) of the underlying variable.<\/span><\/p>\n<p><span style=\"font-weight: 400\">The &#8220;frequency table&#8221; above serves as an example of a genuine histogram, which &#8220;bins&#8221; the range of values first before counting the number of values that fit into each bin. The histogram() method in NumPy accomplishes this, and it serves as the foundation for other functions that you&#8217;ll find in Python libraries like Matplotlib and Pandas that you&#8217;ll encounter later.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">How to create Histogram?<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Creating a bin of the ranges is the first step in creating a histogram. Next, divide the entire range of values into a series of intervals and count the values that fall into each interval.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Bins are distinguished as a series of independent, non-overlapping intervals of variables.<\/span><\/p>\n<p><span style=\"font-weight: 400\">The histogram of x is calculated and produced using the matplotlib.pyplot.hist() function.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Here is a simple histogram:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from matplotlib import pyplot as ptl\nimport numpy as np\n \nxyz = np.array([22, 87, 5, 43, 56,\n              73, 55, 54, 11,\n              20, 51, 5, 79, 31,\n              27])\n \nfig, cb = ptl.subplots(fgs =(10, 7))\ncb.hist(xyz, bins = [0, 25, 50, 75, 100])\nptl.show()\n<\/pre>\n<p><span style=\"font-weight: 400\"><strong>Output<\/strong>: <\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-87231\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/histogram-creation.webp\" alt=\"histogram creation\" width=\"480\" height=\"377\" \/><\/p>\n<h3><span style=\"font-weight: 400\">Histogram personalization:<\/span><\/h3>\n<p><span style=\"font-weight: 400\">To modify the histogram, Matplotlib offers a variety of different techniques. There are numerous attributes available within the matplotlib.pyplot.hist() function itself that allows us to alter a histogram. The hist() function offers a patches object that enables access to the properties of the produced objects, allowing us to alter the plot as we see fit.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as ptl\nImport numpy as np\nfrom matplotlib import colors\nfrom matplotlib.ticker import PercentFormatter\n\nnp.random.seed(23685752)\nnpts = 10000\nnbn= 20\n \n# Creating distribution\nxo = np.random.randn(npts)\nyo = .8 ** x + np.random.randn(10000) + 25\n \nfig, xy = ptl.subplots(1, 1,\n                        fgs =(10, 7),\n                        tight_layout = True)\n \nxy.hist(xo, bins = nbn)\n \nptl1.show()\n<\/pre>\n<p><span style=\"font-weight: 400\"><strong>Output<\/strong>:<\/span><\/p>\n<p>IMAGE<\/p>\n<h3><span style=\"font-weight: 400\">How to plot a KDE: Kernel Density Estimate<\/span><\/h3>\n<p><span style=\"font-weight: 400\">In terms of statistics, you have been using samples throughout this session. Regardless of whether the data is discrete or continuous, it is assumed that it came from a population whose real, precise distribution can be represented by a small number of characteristics.<\/span><\/p>\n<p><span style=\"font-weight: 400\">A method for estimating the probability density function (PDF) of the random variable that &#8220;underlies&#8221; our sample is known as kernel density estimation (KDE). Data smoothing is accomplished using KDE.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Using plot.kde(), which is accessible for both Series and DataFrame objects and part of the Pandas package, you can make &amp; overlay density graphs. However, let&#8217;s first create two separate data samples for comparison:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; means = 10, 20\n&gt;&gt;&gt; sdvs = 4, 2\n&gt;&gt;&gt; dts = pd.DataFrame(\n...     np.random.normal(loc=means, scale=sdvs, size=(1000, 2)),\n...     columns=['a', 'b'])\n&gt;&gt;&gt; dts.agg(['min', 'max', 'mean', 'std']).round(decimals=2)\n<\/pre>\n<p><span style=\"font-weight: 400\">If you look more closely at this function, you can see how closely it resembles the &#8220;real&#8221; PDF for a sample size of only 1000 data points, which is comparatively modest. The &#8220;analytical&#8221; distribution can be created using scipy.stats.norm in the example below (). This class instance includes the moments, descriptive functions, and statistical standard normal distribution. Its PDF is &#8220;accurate&#8221; in that it is precisely specified as the norm. pdf(x) is equal to exp(-x**2\/2)\/sqrt(2*pi).<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Conclusion:<\/span><\/h3>\n<p><span style=\"font-weight: 400\">A frequency distribution table with continuous divisions that have been grouped is represented visually by a histogram. The area diagram consists of a set of rectangles, the areas of which are proportional to the frequency in the corresponding classes, and the foundations are equal to the distances between class limits. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Every rectangle is neighboring because the ground in such representations encompasses the intervals between class limits. For similar groups, rectangle altitudes are inversely correlated with comparable frequencies, and other classes are inversely connected with frequency densities. You learned what a histogram is in this post, along with how to make one in Python using Matplotlib, Pandas, and Seaborn. These libraries each have particular benefits and downsides. Seaborn is the way to go if you&#8217;re seeking a more stats-friendly alternative.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about histograms and how to plot Histogram in Python? Let&#8217;s start!!! What is Histogram? Histograms are a display of statistical information that uses rectangles to show the frequency&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":87221,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[4846],"class_list":["post-86936","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-plot-histogram-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to plot Histogram in Python? - TechVidvan<\/title>\n<meta name=\"description\" content=\"Frequency distribution table with continuous divisions that have been grouped is represented by histogram. Learn to plot histogram in python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to plot Histogram in Python? - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Frequency distribution table with continuous divisions that have been grouped is represented by histogram. Learn to plot histogram in python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-27T03:30:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-histogram.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to plot Histogram in Python? - TechVidvan","description":"Frequency distribution table with continuous divisions that have been grouped is represented by histogram. Learn to plot histogram in python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to plot Histogram in Python? - TechVidvan","og_description":"Frequency distribution table with continuous divisions that have been grouped is represented by histogram. Learn to plot histogram in python.","og_url":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-02-27T03:30:27+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-histogram.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"How to plot Histogram in Python?","datePublished":"2023-02-27T03:30:27+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/"},"wordCount":914,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-histogram.webp","keywords":["plot Histogram in Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/","url":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/","name":"How to plot Histogram in Python? - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-histogram.webp","datePublished":"2023-02-27T03:30:27+00:00","description":"Frequency distribution table with continuous divisions that have been grouped is represented by histogram. Learn to plot histogram in python.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-histogram.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-histogram.webp","width":1200,"height":628,"caption":"python histogram"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/how-to-plot-histogram-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"How to plot Histogram in Python?"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/86936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=86936"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/86936\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/87221"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=86936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=86936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=86936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}