{ "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", "Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.3.0'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly \n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plotly's Figure Factory Module\n", "Plotly's Python API contains a figure factory module which includes many wrapper functions that create unique chart types that are not yet included in [plotly.js](https://github.com/plotly/plotly.js), Plotly's open-source graphing library. The figure factory functions create a full figure, so some Plotly features, such as subplotting, should be implemented slightly differently with these charts." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on package plotly.figure_factory in plotly:\n", "\n", "NAME\n", " plotly.figure_factory\n", "\n", "PACKAGE CONTENTS\n", " _2d_density\n", " _annotated_heatmap\n", " _bullet\n", " _candlestick\n", " _county_choropleth\n", " _dendrogram\n", " _distplot\n", " _facet_grid\n", " _gantt\n", " _ohlc\n", " _quiver\n", " _scatterplot\n", " _streamline\n", " _table\n", " _trisurf\n", " _violin\n", " utils\n", "\n", "DATA\n", " absolute_import = _Feature((2, 5, 0, 'alpha', 1), (3, 0, 0, 'alpha', 0...\n", "\n", "FILE\n", " /home/michael/.virtualenvs/plot3py3/lib/python3.6/site-packages/plotly/figure_factory/__init__.py\n", "\n", "\n" ] } ], "source": [ "import plotly.figure_factory\n", "help(plotly.figure_factory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Vertical Figure Factory Charts\n", "First create the figures that you'd like to appear in the subplot:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "\n", "## Create first plot\n", "x1,y1 = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))\n", "u1 = np.cos(x1)*y1\n", "v1 = np.sin(x1)*y1\n", "\n", "fig1 = ff.create_quiver(x1, y1, u1, v1, name='Quiver')\n", "\n", "\n", "## Create second plot\n", "x = np.linspace(-3, 3, 100)\n", "y = np.linspace(-3, 3, 100)\n", "Y, X = np.meshgrid(x, y)\n", "u = -1 - X**2 + Y\n", "v = 1 + X - Y**2\n", "\n", "fig2 = ff.create_streamline(x, y, u, v, arrow_scale=.1, name='Steamline')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Edit the figures' x and y axes attributes to create subplots:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "for i in range(len(fig1.data)):\n", " fig1.data[i].xaxis='x1'\n", " fig1.data[i].yaxis='y1'\n", "\n", "fig1.layout.xaxis1.update({'anchor': 'y1'})\n", "fig1.layout.yaxis1.update({'anchor': 'x1', 'domain': [.55, 1]})\n", "\n", "for i in range(len(fig2.data)):\n", " fig2.data[i].xaxis='x2'\n", " fig2.data[i].yaxis='y2'\n", "\n", "# initialize xaxis2 and yaxis2\n", "fig2['layout']['xaxis2'] = {}\n", "fig2['layout']['yaxis2'] = {}\n", "\n", "fig2.layout.xaxis2.update({'anchor': 'y2'})\n", "fig2.layout.yaxis2.update({'anchor': 'x2', 'domain': [0, .45]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Combine the data and layout objects to create a figure" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig = go.Figure()\n", "fig.add_traces([fig1.data[0], fig2.data[0]])\n", "\n", "fig.layout.update(fig1.layout)\n", "fig.layout.update(fig2.layout)\n", "\n", "py.iplot(fig, filename='figure_factory_subplot')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Horizontal Table and Chart" ] }, { "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", "import plotly.figure_factory as ff\n", "\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", "\n", "figure = ff.create_table(table_data, height_constant=60)\n", "\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", "\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", "figure.add_traces([trace1, trace2])\n", "\n", "# initialize xaxis2 and yaxis2\n", "figure['layout']['xaxis2'] = {}\n", "figure['layout']['yaxis2'] = {}\n", "\n", "# Edit layout for subplots\n", "figure.layout.xaxis.update({'domain': [0, .5]})\n", "figure.layout.xaxis2.update({'domain': [0.6, 1.]})\n", "\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", "\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", "py.iplot(figure, filename='subplot_table')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Vertical Table and Chart" ] }, { "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", "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", "\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", "\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.add_traces([trace1, trace2])\n", "\n", "# initialize xaxis2 and yaxis2\n", "figure['layout']['xaxis2'] = {}\n", "figure['layout']['yaxis2'] = {}\n", "\n", "# Edit layout for subplots\n", "figure.layout.yaxis.update({'domain': [0, .45]})\n", "figure.layout.yaxis2.update({'domain': [.6, 1]})\n", "\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", "\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", "\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": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/ for more information regarding chart attributes!" ] }, { "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" } ], "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", " \n", "import publisher\n", "publisher.publish(\n", " 'ff-subplots.ipynb', 'python/figure-factory-subplots/', 'Python Figure Factory Subplots | plotly',\n", " 'Subplots with Plotly Figure Factory Charts',\n", " title= 'Figure Factory Subplots in Python | plotly',\n", " name = 'Figure Factory Subplots',\n", " has_thumbnail='true', thumbnail='thumbnail/ff-subplots.jpg',\n", " language='python', \n", " display_as='multiple_axes', order=10,\n", " ipynb= '~PythonPlotBot/1828')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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.15rc1" } }, "nbformat": 4, "nbformat_minor": 2 }