{
"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",
"#### Version Check\n",
"Note: Table traces are available in version 2.2.1+.
\n",
"Run `pip install plotly --upgrade` to update your Plotly version"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2.2.2'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Basic Table"
]
},
{
"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",
"trace = go.Table(\n",
" header=dict(values=['A Scores', 'B Scores']),\n",
" cells=dict(values=[[100, 90, 80, 90],\n",
" [95, 85, 75, 95]]))\n",
"\n",
"data = [trace] \n",
"py.iplot(data, filename = 'basic_table')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Styled Table"
]
},
{
"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",
"trace = go.Table(\n",
" header=dict(values=['A Scores', 'B Scores'],\n",
" line = dict(color='#7D7F80'),\n",
" fill = dict(color='#a1c3d1'),\n",
" align = ['left'] * 5),\n",
" cells=dict(values=[[100, 90, 80, 90],\n",
" [95, 85, 75, 95]],\n",
" line = dict(color='#7D7F80'),\n",
" fill = dict(color='#EDFAFF'),\n",
" align = ['left'] * 5))\n",
"\n",
"layout = dict(width=500, height=300)\n",
"data = [trace]\n",
"fig = dict(data=data, layout=layout)\n",
"py.iplot(fig, filename = 'styled_table')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use a Panda's Dataframe"
]
},
{
"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 pandas as pd\n",
"\n",
"df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv')\n",
"\n",
"trace = go.Table(\n",
" header=dict(values=list(df.columns),\n",
" fill = dict(color='#C2D4FF'),\n",
" align = ['left'] * 5),\n",
" cells=dict(values=[df.Rank, df.State, df.Postal, df.Population],\n",
" fill = dict(color='#F5F8FF'),\n",
" align = ['left'] * 5))\n",
"\n",
"data = [trace] \n",
"py.iplot(data, filename = 'pandas_table')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Changing Row and Column Size"
]
},
{
"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",
"values = [[['Salaries', 'Office', 'Merchandise', 'Legal', 'TOTAL
EXPENSES']],\n",
"[[\"Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad\",\n",
" \"Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad\",\n",
" \"Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad\",\n",
" \"Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad\",\n",
" \"Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad\"]]]\n",
"\n",
"\n",
"trace0 = go.Table(\n",
" columnorder = [1,2],\n",
" columnwidth = [80,400],\n",
" header = dict(\n",
" values = [['EXPENSES
as of July 2017'],\n",
" ['DESCRIPTION']],\n",
" line = dict(color = '#506784'),\n",
" fill = dict(color = '#119DFF'),\n",
" align = ['left','center'],\n",
" font = dict(color = 'white', size = 12),\n",
" height = 40\n",
" ),\n",
" cells = dict(\n",
" values = values,\n",
" line = dict(color = '#506784'),\n",
" fill = dict(color = ['#25FEFD', 'white']),\n",
" align = ['left', 'center'],\n",
" font = dict(color = '#506784', size = 12),\n",
" height = 30\n",
" ))\n",
"\n",
"data = [trace0]\n",
"\n",
"py.iplot(data, filename = \"Row and Column Size\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Alternating Row Colors "
]
},
{
"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",
"headerColor = 'grey'\n",
"rowEvenColor = 'lightgrey'\n",
"rowOddColor = 'white'\n",
"\n",
"trace0 = go.Table(\n",
" header = dict(\n",
" values = [['EXPENSES'],\n",
" ['Q1'],\n",
" ['Q2'],\n",
" ['Q3'],\n",
" ['Q4']],\n",
" line = dict(color = '#506784'),\n",
" fill = dict(color = headerColor),\n",
" align = ['left','center'],\n",
" font = dict(color = 'white', size = 12)\n",
" ),\n",
" cells = dict(\n",
" values = [\n",
" [['Salaries', 'Office', 'Merchandise', 'Legal', 'TOTAL']],\n",
" [[1200000, 20000, 80000, 2000, 12120000]],\n",
" [[1300000, 20000, 70000, 2000, 130902000]],\n",
" [[1300000, 20000, 120000, 2000, 131222000]],\n",
" [[1400000, 20000, 90000, 2000, 14102000]]],\n",
" line = dict(color = '#506784'),\n",
" fill = dict(color = [rowOddColor,rowEvenColor,rowOddColor, rowEvenColor,rowOddColor]),\n",
" align = ['left', 'center'],\n",
" font = dict(color = '#506784', size = 11)\n",
" ))\n",
"\n",
"data = [trace0]\n",
"\n",
"py.iplot(data, filename = \"alternating row colors\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Row Color Based on Variable"
]
},
{
"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 pandas as pd\n",
"import colorlover as cl\n",
"\n",
"colors = cl.scales['5']['seq']['Blues']\n",
"data = {'Year' : [2010, 2011, 2012, 2013, 2014],\n",
" 'Color' : colors}\n",
"df = pd.DataFrame(data)\n",
"\n",
"\n",
"trace0 = go.Table(\n",
" header = dict(\n",
" values = [\"Color\", \"YEAR\"],\n",
" line = dict(color = 'white'),\n",
" fill = dict(color = 'white'),\n",
" align = ['center'],\n",
" font = dict(color = 'black', size = 12)\n",
" ),\n",
" cells = dict(\n",
" values = [df.Color, df.Year],\n",
" line = dict(color = [df.Color]),\n",
" fill = dict(color = [df.Color]),\n",
" align = 'center',\n",
" font = dict(color = 'black', size = 11)\n",
" ))\n",
"\n",
"data = [trace0]\n",
"\n",
"py.iplot(data, filename = \"row variable color\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Cell Color Based on Variable"
]
},
{
"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",
"import numpy as np\n",
"import colorlover as cl\n",
"\n",
"colors = cl.scales['9']['seq']['Reds']\n",
"a = np.random.randint(low=0, high=9, size=10)\n",
"b = np.random.randint(low=0, high=9, size=10)\n",
"c = np.random.randint(low=0, high=9, size=10)\n",
"\n",
"\n",
"trace0 = go.Table(\n",
" header = dict(\n",
" values = ['Column A', 'Column B', 'Column C'],\n",
" line = dict(color = 'white'),\n",
" fill = dict(color = 'white'),\n",
" align = 'center',\n",
" font = dict(color = 'black', size = 12)\n",
" ),\n",
" cells = dict(\n",
" values = [a,b,c],\n",
" line = dict(color = [np.array(colors)[a],np.array(colors)[b],\n",
" np.array(colors)[c]]),\n",
" fill = dict(color = [np.array(colors)[a],np.array(colors)[b],\n",
" np.array(colors)[c]]),\n",
" align = 'center',\n",
" font = dict(color = 'white', size = 11)\n",
" ))\n",
"\n",
"data = [trace0]\n",
"\n",
"py.iplot(data, filename = \"cell variable color\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dash Example"
]
},
{
"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-tableplot) can easily be deployed to a PaaS."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-tableplot/\", width=\"100%\", height=\"850px\", 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-tableplot/code\", width=\"100%\", height=500, frameBorder=\"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"For more information on tables and table attributes see: https://plotly.com/python/reference/#table."
]
},
{
"cell_type": "code",
"execution_count": 3,
"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 /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-t0p8g8mf\n",
"Building wheels for collected packages: publisher\n",
" Building wheel for publisher (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-i9ylj2up/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 19.0.3, 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",
" 'table.ipynb', 'python/table/', 'Python Tables | plotly',\n",
" 'How to make tables in Python with Plotly.',\n",
" title = 'Tables | plotly',\n",
" name = 'Tables',\n",
" thumbnail='thumbnail/table.gif', language='python',\n",
" has_thumbnail='true', display_as='basic', order=7,\n",
" ipynb='~notebook_demo/197')"
]
}
],
"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
}