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.3'

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)


Aw, snap! We don't have an account for ''. Want to try again? You can authenticate with your email address or username. Sign in is not case sensitive.

Don't have an account? plot.ly

Questions? support@plot.ly
---------------------------------------------------------------------------
PlotlyError                               Traceback (most recent call last)
<ipython-input-2-ac0b81ab6c4a> in <module>()
     10     )
     11 ]
---> 12 py.iplot(data)

~/anaconda/lib/python3.6/site-packages/plotly/plotly/plotly.py in iplot(figure_or_data, **plot_options)
    162         embed_options['height'] = str(embed_options['height']) + 'px'
    163 
--> 164     return tools.embed(url, **embed_options)
    165 
    166 

~/anaconda/lib/python3.6/site-packages/plotly/tools.py in embed(file_owner_or_url, file_id, width, height)
    388         else:
    389             url = file_owner_or_url
--> 390         return PlotlyDisplay(url, width, height)
    391     else:
    392         if (get_config_defaults()['plotly_domain']

~/anaconda/lib/python3.6/site-packages/plotly/tools.py in __init__(self, url, width, height)
   1432         def __init__(self, url, width, height):
   1433             self.resource = url
-> 1434             self.embed_code = get_embed(url, width=width, height=height)
   1435             super(PlotlyDisplay, self).__init__(data=self.embed_code)
   1436 

~/anaconda/lib/python3.6/site-packages/plotly/tools.py in get_embed(file_owner_or_url, file_id, width, height)
    293                 "'{1}'."
    294                 "\nRun help on this function for more information."
--> 295                 "".format(url, plotly_rest_url))
    296         urlsplit = six.moves.urllib.parse.urlparse(url)
    297         file_owner = urlsplit.path.split('/')[1].split('~')[1]

PlotlyError: Because you didn't supply a 'file_id' in the call, we're assuming you're trying to snag a figure from a url. You supplied the url, '', we expected it to start with 'https://plot.ly'.
Run help on this function for more information.

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