{ "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: Tables are available in version 2.1.0+
\n", "Run `pip install plotly --upgrade` to update your Plotly version" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.2.2'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Import CSV Data" ] }, { "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", "import pandas as pd\n", "import re\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/Mining-BTC-180.csv')\n", "\n", "# remove min:sec:millisec from dates \n", "for i, row in enumerate(df['Date']):\n", " p = re.compile(' 00:00:00')\n", " datetime = p.split(df['Date'][i])[0]\n", " df.iloc[i, 1] = datetime\n", "\n", "table = go.Table(\n", " columnwidth=[0.4, 0.47, 0.48, 0.4, 0.4, 0.45, 0.5, 0.6],\n", " header=dict(\n", " #values=list(df.columns[1:]),\n", " values=['Date', 'Number
Transactions', 'Output
Volume (BTC)',\n", " 'Market
Price', 'Hash
Rate', 'Cost per
trans-USD',\n", " 'Mining
Revenue-USD', 'Trasaction
fees-BTC'],\n", " font=dict(size=10),\n", " line = dict(color='rgb(50, 50, 50)'),\n", " align = 'left',\n", " fill = dict(color='#d562be'),\n", " ),\n", " cells=dict(\n", " values=[df[k].tolist() for k in df.columns[1:]],\n", " line = dict(color='rgb(50, 50, 50)'),\n", " align = 'left',\n", " fill = dict(color='#f5f5fa')\n", " )\n", ")\n", "py.iplot([table], filename='table-of-mining-data')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Table and Right Aligned Plots\n", "In Plotly there is no native way to insert a Plotly Table into a Subplot. To do this, create your own `Layout` object and defining multiple `xaxis` and `yaxis` to split up the chart area into different domains. Then for the traces you wish to insert in your final chart, set their `xaxis` and `yaxis` individually to map to the domains definied in the `Layout`. See the example below to see how to align 3 Scatter plots to the right and a Table on the top." ] }, { "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", "import pandas as pd\n", "\n", "table_trace1 = go.Table(\n", " domain=dict(x=[0, 0.5],\n", " y=[0, 1.0]),\n", " columnwidth = [30] + [33, 35, 33],\n", " columnorder=[0, 1, 2, 3, 4],\n", " header = dict(height = 50,\n", " values = [['Date'],['Number
transactions
'],\n", " ['Output
volume(BTC)
'], ['Market
Price
']],\n", " line = dict(color='rgb(50, 50, 50)'),\n", " align = ['left'] * 5,\n", " font = dict(color=['rgb(45, 45, 45)'] * 5, size=14),\n", " fill = dict(color='#d562be')),\n", " cells = dict(values = [df[k].tolist() for k in\n", " ['Date', 'Number-transactions', 'Output-volume(BTC)', 'Market-price']],\n", " line = dict(color='#506784'),\n", " align = ['left'] * 5,\n", " font = dict(color=['rgb(40, 40, 40)'] * 5, size=12),\n", " format = [None] + [\", .2f\"] * 2 + [',.4f'],\n", " prefix = [None] * 2 + ['$', u'\\u20BF'],\n", " suffix=[None] * 4,\n", " height = 27,\n", " fill = dict(color=['rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)']))\n", ")\n", "\n", "trace1=go.Scatter(\n", " x=df['Date'],\n", " y=df['Hash-rate'],\n", " xaxis='x1',\n", " yaxis='y1',\n", " mode='lines',\n", " line=dict(width=2, color='#9748a1'),\n", " name='hash-rate-TH/s'\n", ")\n", "\n", "trace2=go.Scatter(\n", " x=df['Date'],\n", " y=df['Mining-revenue-USD'],\n", " xaxis='x2',\n", " yaxis='y2',\n", " mode='lines',\n", " line=dict(width=2, color='#b04553'),\n", " name='mining revenue'\n", ")\n", "\n", "trace3=go.Scatter(\n", " x=df['Date'],\n", " y=df['Transaction-fees-BTC'],\n", " xaxis='x3',\n", " yaxis='y3',\n", " mode='lines',\n", " line=dict(width=2, color='#af7bbd'),\n", " name='transact-fee'\n", ")\n", "\n", "axis=dict(\n", " showline=True,\n", " zeroline=False,\n", " showgrid=True,\n", " mirror=True,\n", " ticklen=4, \n", " gridcolor='#ffffff',\n", " tickfont=dict(size=10)\n", ")\n", "\n", "layout1 = dict(\n", " width=950,\n", " height=800,\n", " autosize=False,\n", " title='Bitcoin mining stats for 180 days',\n", " margin = dict(t=100),\n", " showlegend=False, \n", " xaxis1=dict(axis, **dict(domain=[0.55, 1], anchor='y1', showticklabels=False)),\n", " xaxis2=dict(axis, **dict(domain=[0.55, 1], anchor='y2', showticklabels=False)), \n", " xaxis3=dict(axis, **dict(domain=[0.55, 1], anchor='y3')), \n", " yaxis1=dict(axis, **dict(domain=[0.66, 1.0], anchor='x1', hoverformat='.2f')), \n", " yaxis2=dict(axis, **dict(domain=[0.3 + 0.03, 0.63], anchor='x2', tickprefix='$', hoverformat='.2f')),\n", " yaxis3=dict(axis, **dict(domain=[0.0, 0.3], anchor='x3', tickprefix=u'\\u20BF', hoverformat='.2f')),\n", " plot_bgcolor='rgba(228, 222, 249, 0.65)'\n", ")\n", "\n", "fig1 = dict(data=[table_trace1, trace1, trace2, trace3], layout=layout1)\n", "py.iplot(fig1, filename='table-right-aligned-plots')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### Vertical Table and Graph Subplot" ] }, { "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", "table_trace2 = go.Table(\n", " domain=dict(x=[0, 1],\n", " y=[0.7, 1.0]), \n", " columnwidth=[1, 2, 2, 2],\n", " columnorder=[0, 1, 2, 3, 4],\n", " header = dict(height = 50,\n", " values = [['Date'],['Hash Rate, TH/sec'], \n", " ['Mining revenue'], ['Transaction fees']], \n", " line = dict(color='rgb(50, 50, 50)'),\n", " align = ['left'] * 5,\n", " font = dict(color=['rgb(45, 45, 45)'] * 5, size=14),\n", " fill = dict(color='#d562be')),\n", " cells = dict(values = [df[k].tolist() for k in ['Date', 'Hash-rate', 'Mining-revenue-USD', 'Transaction-fees-BTC']],\n", " line = dict(color='#506784'),\n", " align = ['left'] * 5,\n", " font = dict(color=['rgb(40, 40, 40)'] * 5, size=12),\n", " format = [None] + [\", .2f\"] * 2 + [',.4f'], \n", " prefix = [None] * 2 + ['$', u'\\u20BF'],\n", " suffix=[None] * 4,\n", " height = 27,\n", " fill = dict(color=['rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)']))\n", ")\n", "\n", "trace4=go.Scatter(\n", " x=df['Date'],\n", " y=df['Hash-rate'],\n", " xaxis='x1',\n", " yaxis='y1',\n", " mode='lines',\n", " line=dict(width=2, color='#9748a1'),\n", " name='hash-rate-TH/s'\n", ")\n", "\n", "trace5=go.Scatter(\n", " x=df['Date'],\n", " y=df['Mining-revenue-USD'],\n", " xaxis='x2',\n", " yaxis='y2',\n", " mode='lines',\n", " line=dict(width=2, color='#b04553'),\n", " name='mining revenue'\n", ")\n", "\n", "trace6=go.Scatter(\n", " x=df['Date'],\n", " y=df['Transaction-fees-BTC'],\n", " xaxis='x3',\n", " yaxis='y3',\n", " mode='lines',\n", " line=dict(width=2, color='#af7bbd'),\n", " name='transact-fee'\n", ")\n", "\n", "axis=dict(\n", " showline=True,\n", " zeroline=False,\n", " showgrid=True,\n", " mirror=True, \n", " ticklen=4, \n", " gridcolor='#ffffff',\n", " tickfont=dict(size=10)\n", ")\n", "\n", "layout2 = dict(\n", " width=950,\n", " height=800,\n", " autosize=False,\n", " title='Bitcoin mining stats for 180 days',\n", " margin = dict(t=100),\n", " showlegend=False, \n", " xaxis1=dict(axis, **dict(domain=[0, 1], anchor='y1', showticklabels=False)),\n", " xaxis2=dict(axis, **dict(domain=[0, 1], anchor='y2', showticklabels=False)), \n", " xaxis3=dict(axis, **dict(domain=[0, 1], anchor='y3')), \n", " yaxis1=dict(axis, **dict(domain=[2 * 0.21 + 0.02 + 0.02, 0.68], anchor='x1', hoverformat='.2f')), \n", " yaxis2=dict(axis, **dict(domain=[0.21 + 0.02, 2 * 0.21 + 0.02], anchor='x2', tickprefix='$', hoverformat='.2f')),\n", " yaxis3=dict(axis, **dict(domain=[0.0, 0.21], anchor='x3', tickprefix=u'\\u20BF', hoverformat='.2f')),\n", " plot_bgcolor='rgba(228, 222, 249, 0.65)'\n", ")\n", "\n", "fig2 = dict(data=[table_trace2, trace4, trace5, trace6], layout=layout2)\n", "py.iplot(fig2, filename='vertical-stacked-subplot-tables')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#table for more information regarding chart attributes!
\n", "For examples of Plotly Tables, see: https://plotly.com/python/table/" ] }, { "cell_type": "code", "execution_count": 7, "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/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-EyzCpn-build\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 ... \u001b[?25ldone\n", "\u001b[?25hSuccessfully installed publisher-0.11\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/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", "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/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" ] } ], "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-subplots.ipynb', 'python/table-subplots/', 'Table and Chart Subplots',\n", " 'How to create a subplot with tables and charts in Python with Plotly.',\n", " title = 'Table and Chart Subplots | plotly',\n", " has_thumbnail='true',page_type='example_index', thumbnail='thumbnail/table_subplots.jpg',\n", " language='python',\n", " display_as='multiple_axes', order=11)" ] }, { "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.12" } }, "nbformat": 4, "nbformat_minor": 1 }