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 🔥!

⏱ Quick start (Seaborn)

Seaborn is definitely the best library to quickly build a histogram thanks to its displot().

Note the importance of the bins parameter: try several values to see which represents your data the best. 🔥

# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

# Plot the histogram thanks to the displot function
sns.displot( data=df["sepal_length"], kde=True )

Matplotlib logo Quick start (Matplotlib)

Matplotlib can also build decent histograms easily. It provides a hist() function that accept a vector of numeric values as input.

It also provides all the options you can think of to customize the binning and the genreral appearance.

# 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

Description

Sets the color of the histogram bars.

Possible values → string

Can be a color name, a Hex code, or an RGB value. Learn more about colors.

Code Example

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)
fig, ax = plt.subplots()
ax.hist(data, color="purple")
plt.show()

Pandas logo Quick start (Pandas)

Pandas can build decent histograms easily. It provides different functions like hist() and plot() that need a pandas dataframe (or series) as input.

Since it's based on matplotlib, it provides all the options you can think of to customize the binning and the genreral appearance.