{ "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!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Two Y Axes" ] }, { "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", "trace1 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[40, 50, 60],\n", " name='yaxis data'\n", ")\n", "trace2 = go.Scatter(\n", " x=[2, 3, 4],\n", " y=[4, 5, 6],\n", " name='yaxis2 data',\n", " yaxis='y2'\n", ")\n", "data = [trace1, trace2]\n", "layout = go.Layout(\n", " title='Double Y Axis Example',\n", " yaxis=dict(\n", " title='yaxis title'\n", " ),\n", " yaxis2=dict(\n", " title='yaxis2 title',\n", " titlefont=dict(\n", " color='rgb(148, 103, 189)'\n", " ),\n", " tickfont=dict(\n", " color='rgb(148, 103, 189)'\n", " ),\n", " overlaying='y',\n", " side='right'\n", " )\n", ")\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='multiple-axes-double')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multiple Axes" ] }, { "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", "trace1 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[4, 5, 6],\n", " name='yaxis1 data'\n", ")\n", "trace2 = go.Scatter(\n", " x=[2, 3, 4],\n", " y=[40, 50, 60],\n", " name='yaxis2 data',\n", " yaxis='y2'\n", ")\n", "trace3 = go.Scatter(\n", " x=[4, 5, 6],\n", " y=[40000, 50000, 60000],\n", " name='yaxis3 data',\n", " yaxis='y3'\n", ")\n", "trace4 = go.Scatter(\n", " x=[5, 6, 7],\n", " y=[400000, 500000, 600000],\n", " name='yaxis4 data',\n", " yaxis='y4'\n", ")\n", "data = [trace1, trace2, trace3, trace4]\n", "layout = go.Layout(\n", " title='multiple y-axes example',\n", " width=800,\n", " xaxis=dict(\n", " domain=[0.3, 0.7]\n", " ),\n", " yaxis=dict(\n", " title='yaxis title',\n", " titlefont=dict(\n", " color='#1f77b4'\n", " ),\n", " tickfont=dict(\n", " color='#1f77b4'\n", " )\n", " ),\n", " yaxis2=dict(\n", " title='yaxis2 title',\n", " titlefont=dict(\n", " color='#ff7f0e'\n", " ),\n", " tickfont=dict(\n", " color='#ff7f0e'\n", " ),\n", " anchor='free',\n", " overlaying='y',\n", " side='left',\n", " position=0.15\n", " ),\n", " yaxis3=dict(\n", " title='yaxis4 title',\n", " titlefont=dict(\n", " color='#d62728'\n", " ),\n", " tickfont=dict(\n", " color='#d62728'\n", " ),\n", " anchor='x',\n", " overlaying='y',\n", " side='right'\n", " ),\n", " yaxis4=dict(\n", " title='yaxis5 title',\n", " titlefont=dict(\n", " color='#9467bd'\n", " ),\n", " tickfont=dict(\n", " color='#9467bd'\n", " ),\n", " anchor='free',\n", " overlaying='y',\n", " side='right',\n", " position=0.85\n", " )\n", ")\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='multiple-axes-multiple')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Muliple Y-Axes Subplots" ] }, { "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", "# Top left\n", "trace1 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[2, 32, 62],\n", " name='yaxis data',\n", ")\n", "trace2 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[40, 50, 60],\n", " name='yaxis2 data',\n", " yaxis='y2'\n", ")\n", "\n", "# Top right\n", "trace3 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[2, 32, 62],\n", " name='yaxis3 data',\n", " xaxis='x2',\n", " yaxis='y3'\n", ")\n", "trace4 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[40, 50, 60],\n", " name='yaxis4 data',\n", " xaxis='x2',\n", " yaxis='y4'\n", ")\n", "\n", "# Bottom left\n", "trace5 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[2, 32, 62],\n", " name='yaxis5 data',\n", " xaxis='x3',\n", " yaxis='y5'\n", ")\n", "trace6 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[40, 50, 60],\n", " name='yaxis6 data',\n", " xaxis='x3',\n", " yaxis='y6'\n", ")\n", "\n", "# Bottom right\n", "trace7 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[2, 32, 62],\n", " name='yaxis7 data',\n", " xaxis='x4',\n", " yaxis='y7'\n", ")\n", "trace8 = go.Scatter(\n", " x=[1, 2, 3],\n", " y=[40, 50, 60],\n", " name='yaxis8 data',\n", " xaxis='x4',\n", " yaxis='y8'\n", ")\n", "\n", "\n", "data = [trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8]\n", "layout = go.Layout(\n", " title='Double Y Axis Example',\n", " legend={'x': 1.1},\n", " width=1000,\n", " height=500,\n", " # Top left\n", " xaxis=dict(\n", " title='xaxis title',\n", " domain=[0, 0.4]\n", " ),\n", " yaxis=dict(\n", " title='yaxis title',\n", " type='log',\n", " domain=[0.6, 1.0],\n", " anchor='x'\n", " ),\n", " yaxis2=dict(\n", " title='yaxis2 title',\n", " overlaying='y',\n", " side='right'\n", " ),\n", " \n", " # Top right\n", " xaxis2=dict(\n", " title='xaxis2 title',\n", " domain=[0.6, 1.0],\n", " anchor='y3'\n", " ),\n", " yaxis3=dict(\n", " type='log',\n", " title='yaxis3 title',\n", " domain=[0.6, 1.0],\n", " anchor='x2'\n", " ),\n", " yaxis4=dict(\n", " title='yaxis4 title',\n", " domain=[0.6, 1.0],\n", " overlaying='y3',\n", " side='right',\n", " anchor='x2'\n", " ),\n", " \n", " # Bottom left\n", " xaxis3=dict(\n", " title='xaxis3 title',\n", " domain=[0, 0.4],\n", " anchor='y5'\n", " ),\n", " yaxis5=dict(\n", " type='log',\n", " title='yaxis5 title',\n", " domain=[0, 0.4],\n", " anchor='x3'\n", " ),\n", " yaxis6=dict(\n", " title='yaxis6 title',\n", " domain=[0, 0.4],\n", " overlaying='y5',\n", " side='right',\n", " anchor='x3'\n", " ),\n", " \n", " # Bottom right\n", " xaxis4=dict(\n", " title='xaxis4, title',\n", " domain=[0.6, 1.0],\n", " anchor='y7'\n", " ),\n", " yaxis7=dict(\n", " type='log',\n", " title='yaxis7 title',\n", " domain=[0, 0.4],\n", " anchor='x4'\n", " ),\n", " yaxis8=dict(\n", " title='yaxis8 title',\n", " domain=[0, 0.4],\n", " overlaying='y7',\n", " side='right',\n", " anchor='x4'\n", " )\n", ")\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='multiple-y-axes-subplots')" ] }, { "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-multipleaxesplot) 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-multipleaxesplot/\", width=\"100%\", height=\"650px\", frameBorder=\"0\")\n" ] }, { "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-multipleaxesplot/code\", width=\"100%\", height=500, frameBorder=\"0\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "All of the y-axis properties are found here: https://plotly.com/python/reference/#YAxis" ] }, { "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-lrk9sqom\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-1hdosjeq/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", " 'multiple-axes.ipynb', 'python/multiple-axes/', 'Multiple Axes | Plotly',\n", " 'How to make a graph with multiple axes in python.',\n", " title = 'Python Mulitple Axes | Examples | Plotly',\n", " name = 'Multiple Axes', has_thumbnail='true', thumbnail='thumbnail/multiple-axes.jpg', \n", " language='python',\n", " display_as='file_settings', order=14,\n", " ipynb='~notebook_demo/270')\n" ] } ], "metadata": { "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": 2 }