Histogram

logo of a chart:Histogram

A Histogram represents the distribution of a numeric variable for one or several groups. The values are split in bins, each bin is represented as a bar.

This page showcases many histograms built with python, using the most popular libraries like seaborn and matplotlib.

Examples start with very simple, beginner-friendly histograms and progressively increase in complexity. At the end of the page, some polished & publication-ready histograms are provided, ready to be used in your next project 🔥!

Seaborn logoHistogram charts with Seaborn

Seaborn is a python library allowing to make better charts easily. It is well adapted to build histogram thanks to its displot function. The following charts will guide you through its usage, going from a very basic histogram to something much more customized.

Matplotlib logo Quick start (Matplotlib)

# library & dataset
import matplotlib.pyplot as plt
hours = [17, 20, 22, 25, 26, 27, 30, 31, 32, 38, 40, 40, 45, 55]

# Initialize layout
fig, ax = plt.subplots(figsize = (9, 9))

#plot
ax.hist(hours, bins=5, edgecolor="black");

🔎 hist() function parameters→ see full doc

→ Description

The hist() function of matplotlib creates a histogram to visualize the distribution of a dataset. It plots rectangular bars with heights proportional to the frequency of values in data bins.

→ Arguments

Matplotlib logoHistograms with Matplotlib

As usual matplotlib is perfectly skilled to build nice histogram, but require some more work camparing to seaborn to get a good looking figure.

The examples below should help you to get started with matplotlib histograms. They go from a very basic version and then show how to customize it, like adding annotation.

Pandas logo Quick start (Pandas)

# library & dataset
import pandas as pd
import matplotlib.pyplot as plt

time = [17, 25, 42, 35, 26, 27, 20, 11, 22, 32, 35, 30, 45, 55]

# Convert to a pandas format
time = pd.Series(time)

#plot
time.hist()
plt.show

Pandas logoHistograms with Pandas

Pandas is not the most common Python library to build histograms, but it can be used to build decent ones. It provides different functions like hist() and plot() from matplotlib.

The examples below should help you to get started with basic pandas histograms.

Plotnine logoHistograms with Plotnine

Plotnine allows us to use the grammar-of-graphics to build histograms. It provides a geom_histogram() function that can be used to build histograms.

The examples below should help you to get started with basic plotnine histograms.

Contact


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥