{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/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: Tables are available in version 1.9.2+
\n", "Run `pip install plotly --upgrade` to update your Plotly version" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.4.1'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Simple Table" ] }, { "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.figure_factory as ff\n", "\n", "data_matrix = [['Country', 'Year', 'Population'],\n", " ['United States', 2000, 282200000],\n", " ['Canada', 2000, 27790000],\n", " ['United States', 2005, 295500000],\n", " ['Canada', 2005, 32310000],\n", " ['United States', 2010, 309000000],\n", " ['Canada', 2010, 34000000]]\n", "\n", "table = ff.create_table(data_matrix)\n", "py.iplot(table, filename='simple_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Add Links" ] }, { "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.figure_factory as ff\n", "\n", "data_matrix = [['User', 'Language', 'Chart Type', '# of Views'],\n", " ['empet',\n", " 'Python',\n", " 'Network Graph',\n", " 298],\n", " ['Grondo',\n", " 'Matlab',\n", " 'Subplots',\n", " 356],\n", " ['Dreamshot',\n", " 'Web App',\n", " 'Bubble Map',\n", " 262],\n", " ['FiveThirtyEight',\n", " 'Web App',\n", " 'Scatter',\n", " 692],\n", " ['cpsievert',\n", " 'R',\n", " 'Surface',\n", " 302]]\n", "\n", "table = ff.create_table(data_matrix)\n", "py.iplot(table, filename='linked_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use LaTeX" ] }, { "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.figure_factory as ff\n", "\n", "data_matrix = [['Name', 'Equation'],\n", " ['Pythagorean Theorem', '$a^{2}+b^{2}=c^{2}$'],\n", " ['Euler\\'s Formula', '$F-E+V=2$'],\n", " ['The Origin of Complex Numbers', '$i^{2}=-1$'],\n", " ['Einstein\\'s Theory of Relativity', '$E=m c^{2}$']]\n", "\n", "table = ff.create_table(data_matrix)\n", "py.iplot(table, filename='latex_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use a Panda's Dataframe" ] }, { "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.figure_factory as ff\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')\n", "df_sample = df[100:120]\n", "\n", "table = ff.create_table(df_sample)\n", "py.iplot(table, filename='pandas_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Modify Row Height\n", "The default row height is 30 pixels. Set `height_constant` if you'd like to change the height of each row." ] }, { "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.figure_factory as ff\n", "\n", "data_matrix = [['Country', 'Year', 'Population'],\n", " ['United States', 2000, 282200000],\n", " ['Canada', 2000, 27790000],\n", " ['United States', 2005, 295500000],\n", " ['Canada', 2005, 32310000],\n", " ['United States', 2010, 309000000],\n", " ['Canada', 2010, 34000000]]\n", "\n", "table = ff.create_table(data_matrix, height_constant=20)\n", "py.iplot(table, filename='size_row_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Custom Table Colors\n", "A custom colorscale should be a `list[list]`:
`[[0, 'Header_Color'],[.5, 'Odd_Row_Color'],[1, 'Even_Row_Color']]`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')\n", "df_sample = df[400:410]\n", "\n", "colorscale = [[0, '#4d004c'],[.5, '#f2e5ff'],[1, '#ffffff']]\n", "\n", "table = ff.create_table(df_sample, colorscale=colorscale)\n", "py.iplot(table, filename='color_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Custom Font Colors" ] }, { "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.figure_factory as ff\n", "\n", "text = [['Team', 'Rank'], ['A', 1], ['B', 2], ['C', 3], ['D', 4], ['E', 5], ['F', 6]]\n", "\n", "colorscale = [[0, '#272D31'],[.5, '#ffffff'],[1, '#ffffff']]\n", "font=['#FCFCFC', '#00EE00', '#008B00', '#004F00', '#660000', '#CD0000', '#FF3030']\n", "\n", "table = ff.create_table(text, colorscale=colorscale, font_colors=font)\n", "table.layout.width=250\n", "py.iplot(table, filename='font_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Change Font Size" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "data_matrix = [['Country', 'Year', 'Population'],\n", " ['United States', 2000, 282200000],\n", " ['Canada', 2000, 27790000],\n", " ['United States', 2005, 295500000],\n", " ['Canada', 2005, 32310000],\n", " ['United States', 2010, 309000000],\n", " ['Canada', 2010, 34000000]]\n", "\n", "table = ff.create_table(data_matrix, index=True)\n", "\n", "# Make text size larger\n", "for i in range(len(table.layout.annotations)):\n", " table.layout.annotations[i].font.size = 20\n", "\n", "py.iplot(table, filename='index_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tables with Graphs" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import plotly.figure_factory as ff\n", "\n", "# Add table data\n", "table_data = [['Team', 'Wins', 'Losses', 'Ties'],\n", " ['Montréal
Canadiens', 18, 4, 0],\n", " ['Dallas Stars', 18, 5, 0],\n", " ['NY Rangers', 16, 5, 0], \n", " ['Boston
Bruins', 13, 8, 0],\n", " ['Chicago
Blackhawks', 13, 8, 0],\n", " ['LA Kings', 13, 8, 0],\n", " ['Ottawa
Senators', 12, 5, 0]]\n", "# Initialize a figure with FF.create_table(table_data)\n", "figure = ff.create_table(table_data, height_constant=60)\n", "\n", "# Add graph data\n", "teams = ['Montréal Canadiens', 'Dallas Stars', 'NY Rangers',\n", " 'Boston Bruins', 'Chicago Blackhawks', 'LA Kings', 'Ottawa Senators']\n", "GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 2.45, 3.18]\n", "GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.14, 2.77]\n", "# Make traces for graph\n", "trace1 = go.Scatter(x=teams, y=GFPG,\n", " marker=dict(color='#0099ff'),\n", " name='Goals For
Per Game',\n", " xaxis='x2', yaxis='y2')\n", "trace2 = go.Scatter(x=teams, y=GAPG,\n", " marker=dict(color='#404040'),\n", " name='Goals Against
Per Game',\n", " xaxis='x2', yaxis='y2')\n", "\n", "# Add trace data to figure\n", "figure['data'].extend(go.Data([trace1, trace2]))\n", "\n", "# Edit layout for subplots\n", "figure.layout.xaxis.update({'domain': [0, .5]})\n", "figure.layout.xaxis2.update({'domain': [0.6, 1.]})\n", "# The graph's yaxis MUST BE anchored to the graph's xaxis\n", "figure.layout.yaxis2.update({'anchor': 'x2'})\n", "figure.layout.yaxis2.update({'title': 'Goals'})\n", "# Update the margins to add a title and see graph x-labels. \n", "figure.layout.margin.update({'t':50, 'b':100})\n", "figure.layout.update({'title': '2016 Hockey Stats'})\n", "\n", "# Plot!\n", "py.iplot(figure, filename='subplot_table')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import plotly.figure_factory as FF\n", "\n", "# Add table data\n", "table_data = [['Team', 'Wins', 'Losses', 'Ties'],\n", " ['Montréal
Canadiens', 18, 4, 0],\n", " ['Dallas Stars', 18, 5, 0],\n", " ['NY Rangers', 16, 5, 0], \n", " ['Boston
Bruins', 13, 8, 0],\n", " ['Chicago
Blackhawks', 13, 8, 0],\n", " ['Ottawa
Senators', 12, 5, 0]]\n", "# Initialize a figure with FF.create_table(table_data)\n", "figure = FF.create_table(table_data, height_constant=60)\n", "\n", "# Add graph data\n", "teams = ['Montréal Canadiens', 'Dallas Stars', 'NY Rangers',\n", " 'Boston Bruins', 'Chicago Blackhawks', 'Ottawa Senators']\n", "GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 3.18]\n", "GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.77]\n", "# Make traces for graph\n", "trace1 = go.Bar(x=teams, y=GFPG, xaxis='x2', yaxis='y2',\n", " marker=dict(color='#0099ff'),\n", " name='Goals For
Per Game')\n", "trace2 = go.Bar(x=teams, y=GAPG, xaxis='x2', yaxis='y2',\n", " marker=dict(color='#404040'),\n", " name='Goals Against
Per Game')\n", "\n", "# Add trace data to figure\n", "figure['data'].extend(go.Data([trace1, trace2]))\n", "\n", "# Edit layout for subplots\n", "figure.layout.yaxis.update({'domain': [0, .45]})\n", "figure.layout.yaxis2.update({'domain': [.6, 1]})\n", "# The graph's yaxis2 MUST BE anchored to the graph's xaxis2 and vice versa\n", "figure.layout.yaxis2.update({'anchor': 'x2'})\n", "figure.layout.xaxis2.update({'anchor': 'y2'})\n", "figure.layout.yaxis2.update({'title': 'Goals'})\n", "# Update the margins to add a title and see graph x-labels. \n", "figure.layout.margin.update({'t':75, 'l':50})\n", "figure.layout.update({'title': '2016 Hockey Stats'})\n", "# Update the height because adding a graph vertically will interact with\n", "# the plot height calculated for the table\n", "figure.layout.update({'height':800})\n", "\n", "# Plot!\n", "py.iplot(figure, filename='subplot_table_vertical')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import plotly.figure_factory as ff\n", "\n", "# Add table data\n", "table_data = [['Prominence', 'Percent', 'RGB Value'],\n", " [1, '38%', 'rgb(56, 75, 126)'],\n", " [2, '27%', 'rgb(18, 36, 37)'],\n", " [3, '18%', 'rgb(34, 53, 101)'], \n", " [4, '10%', 'rgb(36, 55, 57)'],\n", " [5, '7%', 'rgb(6, 4, 4)']]\n", "# Initialize a figure with ff.create_table(table_data)\n", "figure = ff.create_table(table_data, height_constant=60)\n", "\n", "# Add graph data\n", "trace1={'labels': ['1st', '2nd', '3rd', '4th', '5th'],\n", " 'values': [38, 27, 18, 10, 7],\n", " 'type': 'pie',\n", " 'name': 'Starry Night',\n", " 'marker': {'colors': ['rgb(56, 75, 126)',\n", " 'rgb(18, 36, 37)',\n", " 'rgb(34, 53, 101)',\n", " 'rgb(36, 55, 57)',\n", " 'rgb(6, 4, 4)']},\n", " 'domain': {'x': [0, 1],\n", " 'y': [.4, 1]},\n", " 'hoverinfo':'label+percent+name',\n", " 'textinfo':'none'\n", " }\n", "\n", "# Add trace data to figure\n", "figure['data'].extend(go.Data([trace1]))\n", "\n", "# Edit layout for subplots\n", "figure.layout.yaxis.update({'domain': [0, .30]})\n", "# The graph's yaxis2 MUST BE anchored to the graph's xaxis2 and vice versa\n", "# Update the margins to add a title and see graph x-labels. \n", "figure.layout.margin.update({'t':75, 'l':50})\n", "figure.layout.update({'title': 'Starry Night'})\n", "# Update the height because adding a graph vertically will interact with\n", "# the plot height calculated for the table\n", "figure.layout.update({'height':800})\n", "\n", "# Plot!\n", "py.iplot(figure)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function create_table in module plotly.figure_factory._table:\n", "\n", "create_table(table_text, colorscale=None, font_colors=None, index=False, index_title='', annotation_offset=0.45, height_constant=30, hoverinfo='none', **kwargs)\n", " BETA function that creates data tables\n", " \n", " :param (pandas.Dataframe | list[list]) text: data for table.\n", " :param (str|list[list]) colorscale: Colorscale for table where the\n", " color at value 0 is the header color, .5 is the first table color\n", " and 1 is the second table color. (Set .5 and 1 to avoid the striped\n", " table effect). Default=[[0, '#66b2ff'], [.5, '#d9d9d9'],\n", " [1, '#ffffff']]\n", " :param (list) font_colors: Color for fonts in table. Can be a single\n", " color, three colors, or a color for each row in the table.\n", " Default=['#000000'] (black text for the entire table)\n", " :param (int) height_constant: Constant multiplied by # of rows to\n", " create table height. Default=30.\n", " :param (bool) index: Create (header-colored) index column index from\n", " Pandas dataframe or list[0] for each list in text. Default=False.\n", " :param (string) index_title: Title for index column. Default=''.\n", " :param kwargs: kwargs passed through plotly.graph_objs.Heatmap.\n", " These kwargs describe other attributes about the annotated Heatmap\n", " trace such as the colorscale. For more information on valid kwargs\n", " call help(plotly.graph_objs.Heatmap)\n", " \n", " Example 1: Simple Plotly Table\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_table\n", " \n", " text = [['Country', 'Year', 'Population'],\n", " ['US', 2000, 282200000],\n", " ['Canada', 2000, 27790000],\n", " ['US', 2010, 309000000],\n", " ['Canada', 2010, 34000000]]\n", " \n", " table = create_table(text)\n", " py.iplot(table)\n", " ```\n", " \n", " Example 2: Table with Custom Coloring\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_table\n", " \n", " text = [['Country', 'Year', 'Population'],\n", " ['US', 2000, 282200000],\n", " ['Canada', 2000, 27790000],\n", " ['US', 2010, 309000000],\n", " ['Canada', 2010, 34000000]]\n", " \n", " table = create_table(text,\n", " colorscale=[[0, '#000000'],\n", " [.5, '#80beff'],\n", " [1, '#cce5ff']],\n", " font_colors=['#ffffff', '#000000',\n", " '#000000'])\n", " py.iplot(table)\n", " ```\n", " Example 3: Simple Plotly Table with Pandas\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_table\n", " \n", " import pandas as pd\n", " \n", " df = pd.read_csv('http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt', sep=' ')\n", " df_p = df[0:25]\n", " \n", " table_simple = create_table(df_p)\n", " py.iplot(table_simple)\n", " ```\n", "\n" ] } ], "source": [ "help(ff.create_table)" ] }, { "cell_type": "code", "execution_count": 16, "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 c:\\users\\brand\\appdata\\local\\temp\\pip-req-build-7k121fq5\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", " Running setup.py install for publisher: started\n", " Running setup.py install for publisher: finished with status 'done'\n", "Successfully installed publisher-0.11\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Python27\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", "\n", "C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:53: UserWarning:\n", "\n", "Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", "\n", "C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:58: UserWarning:\n", "\n", "Your URL has more than 2 parts... are you sure?\n", "\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", " 'ff_table.ipynb', 'python/figure-factory/table/', 'Figure Factory Tables',\n", " \"How to make tables in Python with Plotly's Figure Factory.\",\n", " title = 'Figure Factory Tables | plotly',\n", " thumbnail='thumbnail/table.jpg', language='python',\n", " ipynb='~notebook_demo/13')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.14" } }, "nbformat": 4, "nbformat_minor": 1 }