{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Basic Box Plot ###"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"\n",
"y0 = np.random.randn(50)-1\n",
"y1 = np.random.randn(50)+1\n",
"\n",
"trace0 = go.Box(\n",
" y=y0\n",
")\n",
"trace1 = go.Box(\n",
" y=y1\n",
")\n",
"data = [trace0, trace1]\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Basic Horizontal Box Plot ###"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"\n",
"x0 = np.random.randn(50)\n",
"x1 = np.random.randn(50) + 2\n",
"\n",
"trace0 = go.Box(x=x0)\n",
"trace1 = go.Box(x=x1)\n",
"data = [trace0, trace1]\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Box Plot That Displays the Underlying Data ###"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Box(\n",
" y=[0, 1, 1, 2, 3, 5, 8, 13, 21],\n",
" boxpoints='all',\n",
" jitter=0.3,\n",
" pointpos=-1.8\n",
" )\n",
"]\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Colored Box Plot ###"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"\n",
"y0 = np.random.randn(50)\n",
"y1 = np.random.randn(50)+1\n",
"\n",
"trace0 = go.Box(\n",
" y=y0,\n",
" name = 'Sample A',\n",
" marker = dict(\n",
" color = 'rgb(214, 12, 140)',\n",
" )\n",
")\n",
"trace1 = go.Box(\n",
" y=y1,\n",
" name = 'Sample B',\n",
" marker = dict(\n",
" color = 'rgb(0, 128, 128)',\n",
" )\n",
")\n",
"data = [trace0, trace1]\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Box Plot Styling Mean & Standard Deviation ###"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Box(\n",
" y=[2.37, 2.16, 4.82, 1.73, 1.04, 0.23, 1.32, 2.91, 0.11, 4.51, 0.51, 3.75, 1.35, 2.98, 4.50, 0.18, 4.66, 1.30, 2.06, 1.19],\n",
" name='Only Mean',\n",
" marker=dict(\n",
" color='rgb(8, 81, 156)',\n",
" ),\n",
" boxmean=True\n",
")\n",
"trace1 = go.Box(\n",
" y=[2.37, 2.16, 4.82, 1.73, 1.04, 0.23, 1.32, 2.91, 0.11, 4.51, 0.51, 3.75, 1.35, 2.98, 4.50, 0.18, 4.66, 1.30, 2.06, 1.19],\n",
" name='Mean & SD',\n",
" marker=dict(\n",
" color='rgb(10, 140, 208)',\n",
" ),\n",
" boxmean='sd'\n",
")\n",
"data = [trace0, trace1]\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Styling Outliers ###"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Box(\n",
" y = [0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, \n",
" 8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25],\n",
" name = \"All Points\",\n",
" jitter = 0.3,\n",
" pointpos = -1.8,\n",
" boxpoints = 'all',\n",
" marker = dict(\n",
" color = 'rgb(7,40,89)'),\n",
" line = dict(\n",
" color = 'rgb(7,40,89)')\n",
")\n",
"\n",
"trace1 = go.Box(\n",
" y = [0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, \n",
" 8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25],\n",
" name = \"Only Whiskers\",\n",
" boxpoints = False,\n",
" marker = dict(\n",
" color = 'rgb(9,56,125)'),\n",
" line = dict(\n",
" color = 'rgb(9,56,125)')\n",
")\n",
"\n",
"trace2 = go.Box(\n",
" y = [0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, \n",
" 8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25],\n",
" name = \"Suspected Outliers\",\n",
" boxpoints = 'suspectedoutliers',\n",
" marker = dict(\n",
" color = 'rgb(8,81,156)',\n",
" outliercolor = 'rgba(219, 64, 82, 0.6)',\n",
" line = dict(\n",
" outliercolor = 'rgba(219, 64, 82, 0.6)',\n",
" outlierwidth = 2)),\n",
" line = dict(\n",
" color = 'rgb(8,81,156)')\n",
")\n",
"\n",
"trace3 = go.Box(\n",
" y = [0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, \n",
" 8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25],\n",
" name = \"Whiskers and Outliers\",\n",
" boxpoints = 'outliers',\n",
" marker = dict(\n",
" color = 'rgb(107,174,214)'),\n",
" line = dict(\n",
" color = 'rgb(107,174,214)')\n",
")\n",
"\n",
"data = [trace0,trace1,trace2,trace3]\n",
"\n",
"layout = go.Layout(\n",
" title = \"Box Plot Styling Outliers\"\n",
")\n",
"\n",
"fig = go.Figure(data=data,layout=layout)\n",
"py.iplot(fig, filename = \"Box Plot Styling Outliers\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Grouped Box Plots ###"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"x = ['day 1', 'day 1', 'day 1', 'day 1', 'day 1', 'day 1',\n",
" 'day 2', 'day 2', 'day 2', 'day 2', 'day 2', 'day 2']\n",
"\n",
"trace0 = go.Box(\n",
" y=[0.2, 0.2, 0.6, 1.0, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3],\n",
" x=x,\n",
" name='kale',\n",
" marker=dict(\n",
" color='#3D9970'\n",
" )\n",
")\n",
"trace1 = go.Box(\n",
" y=[0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2],\n",
" x=x,\n",
" name='radishes',\n",
" marker=dict(\n",
" color='#FF4136'\n",
" )\n",
")\n",
"trace2 = go.Box(\n",
" y=[0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1.0, 0.3, 0.6, 0.8, 0.5],\n",
" x=x,\n",
" name='carrots',\n",
" marker=dict(\n",
" color='#FF851B'\n",
" )\n",
")\n",
"data = [trace0, trace1, trace2]\n",
"layout = go.Layout(\n",
" yaxis=dict(\n",
" title='normalized moisture',\n",
" zeroline=False\n",
" ),\n",
" boxmode='group'\n",
")\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Grouped Horizontal Box Plot ###"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" {\n",
" 'x': [0.2, 0.2, 0.6, 1.0, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3],\n",
" 'y': ['day 1', 'day 1', 'day 1', 'day 1', 'day 1', 'day 1', 'day 2', 'day 2', 'day 2', 'day 2', 'day 2', 'day 2'],\n",
" 'name':'kale',\n",
" 'marker': {\n",
" 'color': '#3D9970'\n",
" },\n",
" 'boxmean': False,\n",
" 'orientation': 'h',\n",
" \"type\": \"box\",\n",
" },\n",
" {\n",
" 'x': [0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2],\n",
" 'y': ['day 1', 'day 1', 'day 1', 'day 1', 'day 1', 'day 1', 'day 2', 'day 2', 'day 2', 'day 2', 'day 2', 'day 2'],\n",
" 'name': 'radishes',\n",
" 'marker':{\n",
" 'color': '#FF4136',\n",
" },\n",
" 'boxmean': False,\n",
" 'orientation': 'h',\n",
" \"type\": \"box\",\n",
" },\n",
" {\n",
" 'x': [0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1.0, 0.3, 0.6, 0.8, 0.5],\n",
" 'y': ['day 1', 'day 1', 'day 1', 'day 1', 'day 1', 'day 1', 'day 2', 'day 2', 'day 2', 'day 2', 'day 2', 'day 2'],\n",
" 'name':'carrots',\n",
" 'marker': {\n",
" 'color': '#FF851B',\n",
" },\n",
" 'boxmean': False,\n",
" 'orientation': 'h',\n",
" \"type\": \"box\",\n",
" }\n",
"]\n",
"layout = {\n",
" 'xaxis': {\n",
" 'title': 'normalized moisture',\n",
" 'zeroline': False,\n",
" },\n",
" 'boxmode': 'group',\n",
"}\n",
"fig = go.Figure(data=data, layout=layout)\n",
"\n",
"py.iplot(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rainbow Box Plots ###"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import random\n",
"import plotly.plotly as py\n",
"\n",
"from numpy import * \n",
"\n",
"N = 30 # Number of boxes\n",
"\n",
"# generate an array of rainbow colors by fixing the saturation and lightness of the HSL representation of colour \n",
"# and marching around the hue. \n",
"# Plotly accepts any CSS color format, see e.g. http://www.w3schools.com/cssref/css_colors_legal.asp.\n",
"c = ['hsl('+str(h)+',50%'+',50%)' for h in linspace(0, 360, N)]\n",
"\n",
"# Each box is represented by a dict that contains the data, the type, and the colour. \n",
"# Use list comprehension to describe N boxes, each with a different colour and with different randomly generated data:\n",
"data = [{\n",
" 'y': 3.5*sin(pi * i/N) + i/N+(1.5+0.5*cos(pi*i/N))*random.rand(10), \n",
" 'type':'box',\n",
" 'marker':{'color': c[i]}\n",
" } for i in range(int(N))]\n",
"\n",
"# format the layout\n",
"layout = {'xaxis': {'showgrid':False,'zeroline':False, 'tickangle':60,'showticklabels':False},\n",
" 'yaxis': {'zeroline':False,'gridcolor':'white'},\n",
" 'paper_bgcolor': 'rgb(233,233,233)',\n",
" 'plot_bgcolor': 'rgb(233,233,233)',\n",
" }\n",
"\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fully Styled Box Plots ###\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"x_data = ['Carmelo Anthony', 'Dwyane Wade',\n",
" 'Deron Williams', 'Brook Lopez',\n",
" 'Damian Lillard', 'David West',]\n",
"\n",
"y0 = np.random.randn(50)-1\n",
"y1 = np.random.randn(50)+1\n",
"y2 = np.random.randn(50)\n",
"y3 = np.random.randn(50)+2\n",
"y4 = np.random.randn(50)-2\n",
"y5 = np.random.randn(50)+3\n",
"\n",
"y_data = [y0,y1,y2,y3,y4,y5]\n",
"\n",
"colors = ['rgba(93, 164, 214, 0.5)', 'rgba(255, 144, 14, 0.5)', 'rgba(44, 160, 101, 0.5)', 'rgba(255, 65, 54, 0.5)', 'rgba(207, 114, 255, 0.5)', 'rgba(127, 96, 0, 0.5)']\n",
"\n",
"traces = []\n",
"\n",
"for xd, yd, cls in zip(x_data, y_data, colors):\n",
" traces.append(go.Box(\n",
" y=yd,\n",
" name=xd,\n",
" boxpoints='all',\n",
" jitter=0.5,\n",
" whiskerwidth=0.2,\n",
" fillcolor=cls,\n",
" marker=dict(\n",
" size=2,\n",
" ),\n",
" line=dict(width=1),\n",
" ))\n",
"\n",
"layout = go.Layout(\n",
" title='Points Scored by the Top 9 Scoring NBA Players in 2012',\n",
" yaxis=dict(\n",
" autorange=True,\n",
" showgrid=True,\n",
" zeroline=True,\n",
" dtick=5,\n",
" gridcolor='rgb(255, 255, 255)',\n",
" gridwidth=1,\n",
" zerolinecolor='rgb(255, 255, 255)',\n",
" zerolinewidth=2,\n",
" ),\n",
" margin=dict(\n",
" l=40,\n",
" r=30,\n",
" b=80,\n",
" t=100,\n",
" ),\n",
" paper_bgcolor='rgb(243, 243, 243)',\n",
" plot_bgcolor='rgb(243, 243, 243)',\n",
" showlegend=False\n",
")\n",
"\n",
"fig = go.Figure(data=traces, layout=layout)\n",
"py.iplot(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dash Example\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Dash](https://plotly.com/products/dash/) is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its [source code](https://github.com/plotly/simple-example-chart-apps/tree/master/dash-boxplot) can easily be deployed to a PaaS."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-boxplot/\", width=\"100%\", height=\"650px\", frameBorder=\"0\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-boxplot/code\", width=\"100%\", height=500, frameBorder=\"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#box for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /tmp/pip-req-build-65mlpoiq\n",
"Building wheels for collected packages: publisher\n",
" Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /tmp/pip-ephem-wheel-cache-sgtb8hkk/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
"Successfully built publisher\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.13\n",
" Uninstalling publisher-0.13:\n",
" Successfully uninstalled publisher-0.13\n",
"Successfully installed publisher-0.13\n",
"\u001b[33mYou are using pip version 10.0.1, however version 19.1.1 is available.\n",
"You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'box.ipynb', 'python/box-plots/', 'Box Plots | plotly',\n",
" 'How to make Box Plots in Python with Plotly.',\n",
" title = 'Box Plots | plotly',\n",
" name = 'Box Plots',\n",
" has_thumbnail='true', thumbnail='thumbnail/box.jpg', \n",
" language='python', page_type='example_index',\n",
" display_as='statistical', order=3,\n",
" ipynb='~notebook_demo/20') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}