Show Sidebar Hide Sidebar

Histograms in Python

How to make Histograms in Python with Plotly.

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

Run pip install plotly --upgrade to update your Plotly version

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

Basic Histogram

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x = np.random.randn(500)
data = [go.Histogram(x=x)]

py.iplot(data, filename='basic histogram')
Out[2]:

Normalized Histogram

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x = np.random.randn(500)
data = [go.Histogram(x=x,
                     histnorm='probability')]

py.iplot(data, filename='normalized histogram')
Out[3]:

Horizontal Histogram

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

y = np.random.randn(500)
data = [go.Histogram(y=y)]

py.iplot(data, filename='horizontal histogram')
Out[4]:

Overlaid Histogram

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x0 = np.random.randn(500)
x1 = np.random.randn(500)+1

trace1 = go.Histogram(
    x=x0,
    opacity=0.75
)
trace2 = go.Histogram(
    x=x1,
    opacity=0.75
)

data = [trace1, trace2]
layout = go.Layout(barmode='overlay')
fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='overlaid histogram')
Out[5]:

Stacked Histograms

In [6]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x0 = np.random.randn(500)
x1 = np.random.randn(500)

trace0 = go.Histogram(
    x=x0
)
trace1 = go.Histogram(
    x=x1
)
data = [trace0, trace1]
layout = go.Layout(barmode='stack')
fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='stacked histogram')
Out[6]:

Styled Histogram

In [7]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np
x0 = np.random.randn(500)
x1 = np.random.randn(500)+1

trace1 = go.Histogram(
    x=x0,
    histnorm='percent',
    name='control',
    xbins=dict(
        start=-4.0,
        end=3.0,
        size=0.5
    ),
    marker=dict(
        color='#FFD7E9',
    ),
    opacity=0.75
)
trace2 = go.Histogram(
    x=x1,
    name='experimental',
    xbins=dict(
        start=-3.0,
        end=4,
        size=0.5
    ),
    marker=dict(
        color='#EB89B5'
    ),
    opacity=0.75
)
data = [trace1, trace2]

layout = go.Layout(
    title='Sampled Results',
    xaxis=dict(
        title='Value'
    ),
    yaxis=dict(
        title='Count'
    ),
    bargap=0.2,
    bargroupgap=0.1
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='styled histogram')
Out[7]:

Cumulative Histogram

In [8]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x = np.random.randn(500)
data = [go.Histogram(x=x,
                     cumulative=dict(enabled=True))]

py.iplot(data, filename='cumulative histogram')
Out[8]:

Specify Binning Function

In [9]:
import plotly.plotly as py
import plotly.graph_objs as go

x = ["Apples","Apples","Apples","Oranges", "Bananas"]
y = ["5","10","3","10","5"]

data = [
  go.Histogram(
    histfunc = "count",
    y = y,
    x = x,
    name = "count"
  ),
  go.Histogram(
    histfunc = "sum",
    y = y,
    x = x,
    name = "sum"
  )
]

py.iplot(data, filename='binning function')
Out[9]:

Custom Binning

For custom binning along x-axis, use the attribute nbinsx. Please note that the autobin algorithm will choose a 'nice' round bin size that may result in somewhat fewer than nbinsx total bins. Alternatively, you can set the exact values for xbins along with autobinx = False.

In [10]:
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

x = ['1970-01-01', '1970-01-01', '1970-02-01', '1970-04-01', '1970-01-02', '1972-01-31', '1970-02-13', '1971-04-19']


trace0 = go.Histogram(
    x=x,
    nbinsx = 4,         
  )
trace1 = go.Histogram(
    x=x,
    nbinsx = 8,   
  )
trace2 = go.Histogram(
    x=x,
    nbinsx = 10,     
  )
trace3 = go.Histogram(
    x=x,
    xbins=dict(
        start='1969-11-15',
        end='1972-03-31',
        size= 'M18'),
    autobinx = False
)
trace4 = go.Histogram(
    x=x,
    xbins=dict(
        start='1969-11-15',
        end='1972-03-31',
        size= 'M4'),
    autobinx = False
)
trace5 = go.Histogram(
    x=x,
    xbins=dict(
        start='1969-11-15',
        end='1972-03-31',
        size= 'M2'),
    autobinx = False
)
  
fig = tools.make_subplots(rows=3, cols=2)
fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 2)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 2, 2)
fig.append_trace(trace4, 3, 1)
fig.append_trace(trace5, 3, 2)

py.iplot(fig, filename='custom binning')
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]
[ (3,1) x5,y5 ]  [ (3,2) x6,y6 ]

Out[10]:

Reference

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

Still need help?
Contact Us

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