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

Note: Histograms are available in version 1.9.12+
Run pip install plotly --upgrade to update your Plotly version


In [1]:
import plotly
plotly.__version__


Out[1]:
'2.2.2'

Basic Histogram


In [5]:
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)


Out[5]:

Normalized Histogram


In [6]:
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)


Out[6]:

Horizontal Histogram


In [7]:
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)


Out[7]:

Overlaid Histgram


In [8]:
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)


Out[8]:

Stacked Histograms


In [9]:
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
)
trace2 = go.Histogram(
    x=x1
)
data = [trace1, trace2]
layout = go.Layout(
    barmode='stack'
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig)


Out[9]:

Colored and Styled Histograms


In [10]:
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='count',
    name='control',
    autobinx=False,
    xbins=dict(
        start=-3.2,
        end=2.8,
        size=0.2
    ),
    marker=dict(
        color='fuchsia',
        line=dict(
            color='grey',
            width=0
        )
    ),
    opacity=0.75
)
trace2 = go.Histogram(
    x=x1,
    name='experimental',
    autobinx=False,
    xbins=dict(
        start=-1.8,
        end=4.2,
        size=0.2
    ),
    marker=dict(
        color='rgb(255, 217, 102)'
    ),
    opacity=0.75
)
data = [trace1, trace2]
layout = go.Layout(
    title='Sampled Results',
    xaxis=dict(
        title='Value'
    ),
    yaxis=dict(
        title='Count'
    ),
    barmode='overlay',
    bargap=0.25,
    bargroupgap=0.3
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig)


Out[10]:

Reference

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


In [ ]: