15.1 C
London
Friday, July 5, 2024
HomePythonMatplotlib in PythonHow to Plot Histogram from List of Data in Python

How to Plot Histogram from List of Data in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to plot a histogram from a list of data in Python:

import matplotlib.pyplot as plt

#create list of data
x = [2, 4, 4, 5, 6, 6, 7, 8, 14]

#create histogram from list of data
plt.hist(x, bins=4)

The following examples show how to use this syntax in practice.

Example 1: Create Histogram with Fixed Number of Bins

The following code shows how to create a histogram from a list of data, using a fixed number of bins:

import matplotlib.pyplot as plt

#create list of data
x = [2, 4, 4, 5, 6, 6, 7, 7, 7, 8, 8, 8, 12, 13]

#create histogram with 4 bins
plt.hist(x, bins=4, edgecolor='black')

Example 2: Create Histogram with Specific Bin Ranges

The following code shows how to create a histogram from a list of data, using specified bin ranges:

import matplotlib.pyplot as plt

#create list of data
x = [2, 4, 4, 5, 6, 6, 7, 7, 7, 8, 8, 8, 12, 13]

#specify bin start and end points
bin_ranges = [0, 5, 10, 15]

#create histogram with 4 bins
plt.hist(x, bins=bin_ranges, edgecolor='black')

You can find the complete documentation for the Matplotlib histogram function here.

Additional Resources

The following tutorials explain how to create other commonly used charts in Matplotlib:

How to Plot a Time Series in Matplotlib
How to Create Boxplots by Group in Matplotlib
How to Increase Plot Size in Matplotlib

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories