Show Sidebar Hide Sidebar

Histograms in matplotlib

How to make a histogram in matplotlib. Seven examples of colored, horizontal, and normal histogram bar charts.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'3.1.1'

Basic Histogram With hist Function

In [2]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

gaussian_numbers = np.random.randn(1000)
plt.hist(gaussian_numbers)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")

fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly( fig )
py.iplot(plotly_fig, filename='mpl-basic-histogram')
Out[2]:

Multiple Datasets

Inspired By Stack Overflow.

In [3]:
import plotly.plotly as py
import plotly.tools as tls

import pylab as plt

fig = plt.figure()

x = 0.25*plt.randn(1000)
y = 0.3*plt.randn(1000)
n, bins, patches = plt.hist([x, y])

plotly_fig = tls.mpl_to_plotly( fig )
py.iplot(plotly_fig, filename='mpl-multi-histogram')
Out[3]:

Plot Two Histograms Together

Inspired By Stack Overflow.

In [4]:
import plotly.plotly as py
import plotly.tools as tls

import random
import numpy
import matplotlib.pyplot as plt

histogram=plt.figure()

x = [random.gauss(3,1) for _ in range(400)]
y = [random.gauss(4,2) for _ in range(400)]

bins = numpy.linspace(-10, 10, 100)

plt.hist(x, bins, alpha=0.5)
plt.hist(y, bins, alpha=0.5)

plotly_fig = tls.mpl_to_plotly( histogram )
py.iplot(plotly_fig,  filename='histogram-mpl-same')
Out[4]:

Probability Density Histogram

Inspired by Matplotlib gallery.

In [5]:
import plotly.plotly as py
import plotly.tools as tls

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

fig = plt.figure()

# example data
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)

num_bins = 50
# the histogram of the data
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins, y, 'r--')
plt.xlabel('Smarts')
plt.ylabel('Probability')

# Tweak spacing to prevent clipping of ylabel
plt.subplots_adjust(left=0.15)

plotly_fig = tls.mpl_to_plotly( fig )
py.iplot(plotly_fig, filename='histogram-mpl-legend')
Out[5]:

Numpy Histogram

Inspired By Stack Overflow.

In [6]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

numpy_hist = plt.figure()

plt.hist([1, 2, 1], bins=[0, 1, 2, 3])

plotly_fig = tls.mpl_to_plotly( numpy_hist )
py.iplot(plotly_fig, filename='numpy-bins')
Out[6]:

Reference

See https://plot.ly/python/reference/#histogram and https://plot.ly/python/histograms/ for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.