Skip to main content
TopMiniSite

Back to all posts

How to Create A Histogram In Matplotlib?

Published on
4 min read
How to Create A Histogram In Matplotlib? image

Best Matplotlib Guides to Buy in October 2025

1 Effective Visualization: Exploiting Matplotlib & Pandas (Treading on Python)

Effective Visualization: Exploiting Matplotlib & Pandas (Treading on Python)

BUY & SAVE
$49.00
Effective Visualization: Exploiting Matplotlib & Pandas (Treading on Python)
2 Data Visualization in Python with Matplotlib: The Complete Guide to Mastering Python

Data Visualization in Python with Matplotlib: The Complete Guide to Mastering Python

BUY & SAVE
$9.99
Data Visualization in Python with Matplotlib: The Complete Guide to Mastering Python
3 Mastering Data Analysis with Python: A Comprehensive Guide to NumPy, Pandas, and Matplotlib

Mastering Data Analysis with Python: A Comprehensive Guide to NumPy, Pandas, and Matplotlib

BUY & SAVE
$19.99
Mastering Data Analysis with Python: A Comprehensive Guide to NumPy, Pandas, and Matplotlib
4 Python Data Analytics and Visualization for Beginners: A Hands-On Guide to Exploring and Visualizing Data with Pandas and Matplotlib (Python MEGA bundle)

Python Data Analytics and Visualization for Beginners: A Hands-On Guide to Exploring and Visualizing Data with Pandas and Matplotlib (Python MEGA bundle)

BUY & SAVE
$24.99
Python Data Analytics and Visualization for Beginners: A Hands-On Guide to Exploring and Visualizing Data with Pandas and Matplotlib (Python MEGA bundle)
5 Data Analysis Foundations with Python: Master Python and Data Analysis using NumPy, Pandas, Matplotlib, and Seaborn: A Hands-On Guide with Projects ... From Basics to Real-World Applications)

Data Analysis Foundations with Python: Master Python and Data Analysis using NumPy, Pandas, Matplotlib, and Seaborn: A Hands-On Guide with Projects ... From Basics to Real-World Applications)

BUY & SAVE
$39.90
Data Analysis Foundations with Python: Master Python and Data Analysis using NumPy, Pandas, Matplotlib, and Seaborn: A Hands-On Guide with Projects ... From Basics to Real-World Applications)
6 Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)

Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)

BUY & SAVE
$23.17 $39.95
Save 42%
Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)
7 Python Cheat Sheet, Guide by Examples, Cover all Basic Python Syntaxes, Complete Reference (2025.01): Python Programming Syntax Table & Chart, Quick Study Workbook, Syntax Dictionary

Python Cheat Sheet, Guide by Examples, Cover all Basic Python Syntaxes, Complete Reference (2025.01): Python Programming Syntax Table & Chart, Quick Study Workbook, Syntax Dictionary

BUY & SAVE
$19.99
Python Cheat Sheet, Guide by Examples, Cover all Basic Python Syntaxes, Complete Reference (2025.01): Python Programming Syntax Table & Chart, Quick Study Workbook, Syntax Dictionary
8 Python Data Mining Quick Start Guide: A beginner's guide to extracting valuable insights from your data

Python Data Mining Quick Start Guide: A beginner's guide to extracting valuable insights from your data

BUY & SAVE
$24.85 $32.99
Save 25%
Python Data Mining Quick Start Guide: A beginner's guide to extracting valuable insights from your data
+
ONE MORE?

To create a histogram in Matplotlib, you can follow the following steps:

  1. Import the necessary libraries:

import matplotlib.pyplot as plt import numpy as np

  1. Prepare the data: Create a list or array of numeric values that you want to display as a histogram. For example:

data = [1, 2, 3, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 9, 10]

  1. Create the histogram plot:

plt.hist(data, bins=10, edgecolor='black')

In this example, we pass the data and specify the number of bins (which determines the width and range of each bar), as well as the edgecolor for the bars.

  1. Customize the appearance: You can adjust various aspects of the histogram by using additional Matplotlib functions. For instance, you can set the title, x-axis label, and y-axis label:

plt.title('Histogram') plt.xlabel('Value') plt.ylabel('Frequency')

  1. Display the plot: Finally, use the plt.show() function to display the histogram:

plt.show()

With these steps, you should be able to create a basic histogram in Matplotlib. Remember to customize the plot further according to your desired requirements.

What is the function to create a histogram in Matplotlib?

The function to create a histogram in Matplotlib is matplotlib.pyplot.hist().

How to adjust the transparency of a histogram's bars in Matplotlib?

To adjust the transparency of a histogram's bars in Matplotlib, you can use the alpha parameter when plotting the histogram using the hist function. The alpha parameter controls the opacity of the bars and takes values between 0 and 1, where 0 is completely transparent and 1 is completely opaque.

Here's a code example:

import matplotlib.pyplot as plt

Generate some random data

data = [1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 2, 3, 4, 5, 5]

Plot the histogram with transparency

plt.hist(data, bins=5, alpha=0.5)

Show the plot

plt.show()

In this example, alpha=0.5 makes the bars 50% transparent. You can adjust the value of alpha to control the desired transparency level.

How to save a histogram plot as an image file using Matplotlib?

To save a histogram plot as an image file using Matplotlib, you can use the savefig() function.

First, you need to import the required libraries:

import matplotlib.pyplot as plt

Then, you can create your histogram plot. Here's an example:

# Create a list of data points data = [1, 2, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 9, 10]

Create a histogram plot

plt.hist(data, bins=5)

Set labels and title

plt.xlabel('Value') plt.ylabel('Frequency') plt.title('Histogram')

Save the histogram plot as an image file

plt.savefig('histogram.png')

In this example, we create a histogram plot with some sample data points. We set the number of bins to 5 using the bins parameter. Then, we set labels for the x and y axes and give the plot a title.

Finally, we use the savefig() function to save the histogram plot as an image file. The argument passed to savefig() specifies the file name and the file format (e.g., 'histogram.png', 'histogram.jpg', etc.).

Make sure to call savefig() before calling show() if you also want to display the plot on the screen.

Note: If you are using Jupyter Notebook or a similar environment, you might need to call plt.close() after saving the plot to prevent it from being displayed twice.

How to add a title to a histogram in Matplotlib?

To add a title to a histogram in Matplotlib, you can use the set_title() method of the Axes object.

Here's an example code to demonstrate how to add a title to a histogram:

import matplotlib.pyplot as plt

Generate some random data for the histogram

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]

Create a figure and axes

fig, ax = plt.subplots()

Plot the histogram

ax.hist(data, bins=5)

Set the title

ax.set_title('Histogram of Data')

Display the histogram

plt.show()

In the above code, we first import the matplotlib.pyplot module. Then we create some random data for the histogram.

Next, we create a figure and axes using plt.subplots(). We plot the histogram using the hist() method of the axes object, specifying the data and the desired number of bins.

Finally, we use the set_title() method of the axes object to set the title of the histogram. We pass the desired title as a string.

You can then display the histogram using plt.show().