Udit Vashisht
Author: Udit Vashisht


Python Histograms | Matplotlib Tutorial in Python | Chapter 6

  • 4 minutes read
  • 32 Views
Python Histograms | Matplotlib Tutorial in Python | Chapter 6

Matplotlib Tutorial in Python

Python Histograms | Chapter 6

A Python Histogram/Matplotlib Histogram is an accurate representation of the distribution of numerical data. Bar graphsrelates to two variables, however a Python Histogram relates to only one variable and shows it distribution in different range of the values called “bin” or “bucket”.

Python Histogram in Matplotlib

To create as simple Python Histogram in Matplotlib we will be using plt.hist(). Let us quickly right the following code to plot a simple Python/Matplolib Histogram of ages in Matplotlib.

# python_histogram.py

import matplotlib.pyplot as plt

plt.style.use('ggplot')
ages = [12, 13, 15, 19, 20, 20, 24, 26, 32, 39, 49]
plt.hist(ages)
plt.xlabel("Age")
plt.ylabel("Age Disribution")
plt.title("Age Distribution Histogram")
plt.show()

matplotlib_tutorial_histogram_.png

Python Histogram with bins in Matplotlib

The above Python Histogram is an-auto Python Histogram created by the Matplolib. Now we will define the bins and pass it to plt.hist() to create the Matplotlib Histogram. We can simply choose the number of bins and Matplotlib will distribute the data on the basis of the number of bins provided in the Python Histogram. We will also add border to each bin so that we can distinguish between them easily.

# python_histogram.py

bins = 5
plt.hist(ages, bins = bins, edgecolor = 'black')

matplotlib_tutorial_histogram_bins.png

So, in the above Matplotlib/Python Histogram, the age-range is evenly distributed between 5 bins and each bin has certain number of persons corresponding to that age range.

Instead of Matplotlib doing the distribution in the Histogram, we can also provide the age-range in form of a list.

# python_histogram.py

ages = [12, 13, 15, 19, 20, 20, 24, 26, 32, 39, 49]
bins = [10, 15, 20, 25, 30, 35, 40, 45, 50]
plt.hist(ages, bins=bins, edgecolor='black')

matplotlib_tutorial_histogram_bins_list.png.

So, here instead of providing number of bins, we have provided the list, where two adjacent items represent one bin of that age-group. So, we have 8 age-groups and the Python Histrogram shows the distribution of person in those 8 age groups. We can also, ignore certain ages by just removing them from the bin-range.

# python_histogram.py
ages = [12, 13, 15, 19, 20, 20, 24, 26, 32, 39, 49]
bins = [20, 25, 30, 35, 40, 45, 50]
plt.hist(ages, bins=bins, edgecolor='black')

matplotlib_tutorial_histogram__.png

So, here we have removed two age-bins (age-ranges) i.e. 10-15 and 15-20. Though 12,13,15 are present in the ages but still they are not plotted on the histogram.

Customizing the Python Histogram in Matplotlib

We can further customize our Histograms, like we can draw a verticle line for median age using plt.axvline().

# python_histogram.py

import matplotlib.pyplot as plt

plt.style.use('ggplot')
ages = [12, 13, 15, 19, 20, 20, 24, 26, 32, 39, 49]
bins = [10, 15, 20, 25, 30, 35, 40, 45, 50]
median_age = 20
plt.hist(ages, bins=bins, edgecolor='black')
plt.axvline(median_age, color='blue', label="Median Age")
plt.xlabel("Age")
plt.ylabel("Age Disribution")
plt.title("Age Distribution Histogram")
plt.legend()
plt.show()

matplotlib_tutorial_histogram_!.png

Table of Contents of Matplotlib Tutorial in Python

Matplotlib Tutorial in Python | Chapter 1 | Introduction

Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3

Matplotlib Stack Plots/Bars | Matplotlib Tutorial in Python | Chapter 4

Filling Area on Line Plots | Matplotlib Tutorial in Python | Chapter 5

Python Histograms | Matplotlib Tutorial in Python | Chapter 6

Scatter Plotting in Python | Matplotlib Tutorial | Chapter 7

Plot Time Series in Python | Matplotlib Tutorial | Chapter 8

Python Realtime Plotting | Matplotlib Tutorial | Chapter 9

Matplotlib Subplot in Python | Matplotlib Tutorial | Chapter 10

Python Candlestick Chart | Matplotlib Tutorial | Chapter 11

So, here we have a median line of blue color at age 30.

If you have liked our tutorial, there are various ways to support us, the easiest is to share this post. You can also follow us on facebook, twitter and youtube.

In case of any query, you can leave the comment below.

In the next chapter we will be learning to create scatterplots in Matplotlib in Python.

If you want to support our work. You can do it using Patreon.



Related Posts

Matplotlib Subplot in Python | Matplotlib Tutorial | Chapter 10
By Udit Vashisht

Matplotlib Subplot in Python | Chapter 10

In this Matplotlib Subplot tutorial, we will be learning to create Matplotlib Subplots. Till now, we have been using matplotlib.pyplot() to create the plots. But now, we will be using matplotlib.pyplot.subplots() (ptl.subplots()) to create Matplotlib Subplots.

Matplotlib.pyplot() or plt was automatically ...

Read More
Chapter 6 - Data Types & Variables
By Udit Vashisht

Variables

If we go by the dictionary meaning ‘Variable’ is something which is ‘able to be changed or adapted’. Which is true to much extent in terms of Python also. Variable is basically a reference to the memory location where an object is stored by a computer program. The names ...

Read More
Python Tutorial for Beginners
By Udit Vashisht

Python Tutorial for Beginners

Have you heard a lot about Python Language? Are you looking for free and reliable resource to learn Python? If Yes, your search for the Best Python Tutorial is over.

We are excited to bring an exhaustive Python tutorial for a complete beginner. Even ...

Read More
Search