Plotly's Python library is free and open source! Get started by dowloading 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!
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.tools import FigureFactory as FF
import numpy as np
import pandas as pd
import scipy
Let us import a dataset to perform our statistics. We will be looking at the consumption of alcohol by country in 2010.
data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2010_alcohol_consumption_by_country.csv')
df = data[0:10]
table = FF.create_table(df)
py.iplot(table, filename='alcohol-data-sample')
Two of the most basic statistical operations are the mean
$\mu$ and standard deviation
$\sigma$ of a one-dimension array of data, that is, a sequence of numeric values. The mean
of a set of numbers $x_1, ..., x_N$ is defined as:
The mean is used colloquially as the average of a set of values. The standard deviation on the other hand is a statistical metric that describes the spread of the data, or how far the values are from the mean. The standard deviation
of a set of data is defined as:
mean = np.mean(data['alcohol'])
st_dev = np.std(data['alcohol'])
print("The mean is %r") %(mean)
print("The standard deviation is %r") %(st_dev)
The mean is 6.2083769633507835 The standard deviation is 4.130671000635401
We can also compute other statistics such as the median
, maximum
and minimum
of the data
median = np.median(data['alcohol'])
maximum = np.max(data['alcohol'])
minimum = np.min(data['alcohol'])
print("The median is %r") %(median)
print("The maximum is %r") %(maximum)
print("The minimum is %r") %(minimum)
The median is 6.4000000000000004 The maximum is 17.5 The minimum is 0.10000000000000001
We can visualize these statistics by producing a Plotly box or Violin chart.
y = data['alcohol'].values.tolist()
fig = FF.create_violin(y, title='Violin Plot', colors='#604d9e')
py.iplot(fig, filename='alcohol-violin-visual')
y = data['alcohol'].values.tolist()
trace = go.Box(
y=y,
name = 'Box Plot',
boxpoints='all',
jitter=0.3,
marker = dict(
color = 'rgb(214,12,140)',
),
)
layout = go.Layout(
width=500,
yaxis=dict(
title='Alcohol Consumption by Country',
zeroline=False
),
)
data = [trace]
fig= go.Figure(data=data, layout=layout)
py.iplot(fig, filename='alcohol-box-plot')
from IPython.display import display, HTML
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
'python-Basic-Statistics.ipynb', 'python/basic-statistics/', 'Basic Statistics | plotly',
'Learn how to perform basic statistical operations using Python.',
title='Basic Statistics in Python. | plotly',
name='Basic Statistics',
language='python',
page_type='example_index', has_thumbnail='false', display_as='statistics', order=1,
ipynb= '~notebook_demo/109')
Collecting git+https://github.com/plotly/publisher.git Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-ULX1Fx-build Installing collected packages: publisher Found existing installation: publisher 0.10 Uninstalling publisher-0.10: Successfully uninstalled publisher-0.10 Running setup.py install for publisher ... - \ done Successfully installed publisher-0.10
/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead. "You should import from nbconvert instead.", ShimWarning) /Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you "Save" this notebook before running this command? Remember to save, always save. warnings.warn('Did you "Save" this notebook before running this command? '