GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All
  • Best
  • Libs
  • Learn

This view

  • Original page
  • New address

GeezerVison is showing python-graph-gallery.com — Python Histogram Gallery | Dozens of examples with code

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

Histogram sectionAbout this chart

⏱ 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. 🔥

The most basic histogram one can make with python and seaborn

The most basic histogram one can make with python and seaborn

# 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 )

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.

The most basic histogram with seaborn. It adds a density on top by default

The most basic histogram with seaborn. It adds a density on top by default

Play with the bin parameter to control the number of bars

Play with the bin parameter to control the number of bars

Remove the density chart on top of the seaborn histogram

Remove the density chart on top of the seaborn histogram

Add rug on top of the histogram

Add rug on top of the histogram

Customize the rug: color, size and more

Customize the rug: color, size and more

Customize the appearance of the density chart displayed on top of the histogram

Customize the appearance of the density chart displayed on top of the histogram

Flipping the histogram to display it horizontally

Flipping the histogram to display it horizontally

How to split the chart window to display a boxplot on top of the histogram

How to split the chart window to display a boxplot on top of the histogram

Compare the distribution of several groups on the same chart

Compare the distribution of several groups on the same chart

Use small multiple to compare the distribution of several groups

Use small multiple to compare the distribution of several groups

Histogram can be added around a scatterplot to show each variable distributions

Histogram can be added around a scatterplot to show each variable distributions

Histogram can be added around a 2d density chart to show x and y variable distributions

Histogram can be added around a 2d density chart to show x and y variable distributions

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.

The most basic histogram one can make with python and matplotlib

The most basic histogram one can make with python and 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

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()
More about the hist() function.

More about the hist() function.

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.

The most basic histogram with matplotlib.

The most basic histogram with matplotlib.

Reduce chart opacity and add annotation (median represented in vertical line)

Reduce chart opacity and add annotation (median represented in vertical line)

Cumulative histogram

Cumulative histogram

Mirror density chart to compare the distribution of 2 variables

Mirror density chart to compare the distribution of 2 variables

A histogram with bars being colored differently based on thresholds

A histogram with bars being colored differently based on thresholds

Use small multiple to compare the distribution of several groups or several variables

Use small multiple to compare the distribution of several groups or several variables

Subplot, title, and margin customization

Subplot, title, and margin customization

Add patterns to your histogram bars

Add patterns to your histogram bars

⚠️ Avoid the 61 most common dataviz mistakes

Boxplots hiding information, pie charts, cut axes, spaghetti charts... I gathered the 61 dataviz caveats I see the most as a deck of cards. Each card shows a chart that is not ideal, and a better alternative.

Get the full deck for free:

I respect your privacy. Unsubscribe anytime.

Dataviz caveat card: Histograms: mind the bin size
61
cards

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.

The most basic histogram one can make with python and pandas

The most basic histogram one can make with python and 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.

The most basic histogram with pandas

The most basic histogram with pandas

Customize title, labels and bins of histograms with pandas

Customize title, labels and bins of histograms with pandas

Show the distribution of 2 groups or variables on a single graph

Show the distribution of 2 groups or variables on a single graph

Use small multiple to compare the distribution of several groups on the same chart

Use small multiple to compare the distribution of several groups on the same chart

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.

Most basic histogram with plotnine

Most basic histogram with plotnine

Control bar color

Control bar color

Custom style with theme, colors and opacity

Custom style with theme, colors and opacity

Split histogram by group

Split histogram by group

Mirror histogram to compare 2 variables

Mirror histogram to compare 2 variables

Matplotlib logoBest python histogram examples

The web is full of astonishing charts made by awesome bloggers, (often using R). The Python graph gallery tries to display (or translate from R) some of the best creations and explain how their source code works. If you want to display your work here, please drop me a word or even better, submit a Pull Request!

Nice histogram with custom annotations.

Nice histogram with custom annotations.

Related chart types

logo of a chart:Violin

Violin

logo of a chart:Density

Density

logo of a chart:Histogram

Histogram

logo of a chart:Box1

Boxplot

logo of a chart:Joyplot

Ridgeline

logo of a chart:Beeswarm2Big

Beeswarm

300 projects

💡 Out of dataviz inspiration?

I have spent years collecting the most beautiful data visualizations out there. Get the list of my top 300 dataviz projects, hand-picked, for free.

I respect your privacy. Unsubscribe anytime.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.