{ "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": [ "#### Version Check" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.1.0'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Simple Line Plot" ] }, { "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", "# Create random data with numpy\n", "import numpy as np\n", "\n", "N = 500\n", "random_x = np.linspace(0, 1, N)\n", "random_y = np.random.randn(N)\n", "\n", "# Create a trace\n", "trace = go.Scatter(\n", " x = random_x,\n", " y = random_y\n", ")\n", "\n", "data = [trace]\n", "\n", "py.iplot(data, filename='basic-line')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Line Plot Modes" ] }, { "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", "# Create random data with numpy\n", "import numpy as np\n", "\n", "N = 100\n", "random_x = np.linspace(0, 1, N)\n", "random_y0 = np.random.randn(N)+5\n", "random_y1 = np.random.randn(N)\n", "random_y2 = np.random.randn(N)-5\n", "\n", "# Create traces\n", "trace0 = go.Scatter(\n", " x = random_x,\n", " y = random_y0,\n", " mode = 'lines',\n", " name = 'lines'\n", ")\n", "trace1 = go.Scatter(\n", " x = random_x,\n", " y = random_y1,\n", " mode = 'lines+markers',\n", " name = 'lines+markers'\n", ")\n", "trace2 = go.Scatter(\n", " x = random_x,\n", " y = random_y2,\n", " mode = 'markers',\n", " name = 'markers'\n", ")\n", "data = [trace0, trace1, trace2]\n", "\n", "py.iplot(data, filename='line-mode')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Style Line Plots\n", "This example styles the color and dash of the traces, adds trace names, \n", "modifiys line width, and adds plot and axes titles." ] }, { "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", "# Add data\n", "month = ['January', 'February', 'March', 'April', 'May', 'June', 'July',\n", " 'August', 'September', 'October', 'November', 'December']\n", "high_2000 = [32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3]\n", "low_2000 = [13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9]\n", "high_2007 = [36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0]\n", "low_2007 = [23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6]\n", "high_2014 = [28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9]\n", "low_2014 = [12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1]\n", "\n", "# Create and style traces\n", "trace0 = go.Scatter(\n", " x = month,\n", " y = high_2014,\n", " name = 'High 2014',\n", " line = dict(\n", " color = ('rgb(205, 12, 24)'),\n", " width = 4)\n", ")\n", "trace1 = go.Scatter(\n", " x = month,\n", " y = low_2014,\n", " name = 'Low 2014',\n", " line = dict(\n", " color = ('rgb(22, 96, 167)'),\n", " width = 4,)\n", ")\n", "trace2 = go.Scatter(\n", " x = month,\n", " y = high_2007,\n", " name = 'High 2007',\n", " line = dict(\n", " color = ('rgb(205, 12, 24)'),\n", " width = 4,\n", " dash = 'dash') # dash options include 'dash', 'dot', and 'dashdot'\n", ")\n", "trace3 = go.Scatter(\n", " x = month,\n", " y = low_2007,\n", " name = 'Low 2007',\n", " line = dict(\n", " color = ('rgb(22, 96, 167)'),\n", " width = 4,\n", " dash = 'dash')\n", ")\n", "trace4 = go.Scatter(\n", " x = month,\n", " y = high_2000,\n", " name = 'High 2000',\n", " line = dict(\n", " color = ('rgb(205, 12, 24)'),\n", " width = 4,\n", " dash = 'dot')\n", ")\n", "trace5 = go.Scatter(\n", " x = month,\n", " y = low_2000,\n", " name = 'Low 2000',\n", " line = dict(\n", " color = ('rgb(22, 96, 167)'),\n", " width = 4,\n", " dash = 'dot')\n", ")\n", "data = [trace0, trace1, trace2, trace3, trace4, trace5]\n", "\n", "# Edit the layout\n", "layout = dict(title = 'Average High and Low Temperatures in New York',\n", " xaxis = dict(title = 'Month'),\n", " yaxis = dict(title = 'Temperature (degrees F)'),\n", " )\n", "\n", "fig = dict(data=data, layout=layout)\n", "py.iplot(fig, filename='styled-line')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Connect Data Gaps" ] }, { "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, 4, 5, \n", " 6, 7, 8, 9, 10,\n", " 11, 12, 13, 14, 15],\n", " y=[10, 20, None, 15, 10,\n", " 5, 15, None, 20, 10,\n", " 10, 15, 25, 20, 10],\n", " name = 'No Gaps', # Style name/legend entry with html tags\n", " connectgaps=True\n", ")\n", "trace2 = go.Scatter(\n", " x=[1, 2, 3, 4, 5,\n", " 6, 7, 8, 9, 10,\n", " 11, 12, 13, 14, 15],\n", " y=[5, 15, None, 10, 5,\n", " 0, 10, None, 15, 5,\n", " 5, 10, 20, 15, 5],\n", " name = 'Gaps',\n", ")\n", "\n", "data = [trace1, trace2]\n", "\n", "fig = dict(data=data)\n", "py.iplot(fig, filename='simple-connectgaps')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Interpolation with Line Plots" ] }, { "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, 4, 5],\n", " y=[1, 3, 2, 3, 1],\n", " mode='lines+markers',\n", " name=\"'linear'\",\n", " hoverinfo='name',\n", " line=dict(\n", " shape='linear'\n", " )\n", ")\n", "trace2 = go.Scatter(\n", " x=[1, 2, 3, 4, 5],\n", " y=[6, 8, 7, 8, 6],\n", " mode='lines+markers',\n", " name=\"'spline'\",\n", " text=[\"tweak line smoothness
with 'smoothing' in line object\"],\n", " hoverinfo='text+name',\n", " line=dict(\n", " shape='spline'\n", " )\n", ")\n", "trace3 = go.Scatter(\n", " x=[1, 2, 3, 4, 5],\n", " y=[11, 13, 12, 13, 11],\n", " mode='lines+markers',\n", " name=\"'vhv'\",\n", " hoverinfo='name',\n", " line=dict(\n", " shape='vhv'\n", " )\n", ")\n", "trace4 = go.Scatter(\n", " x=[1, 2, 3, 4, 5],\n", " y=[16, 18, 17, 18, 16],\n", " mode='lines+markers',\n", " name=\"'hvh'\",\n", " hoverinfo='name',\n", " line=dict(\n", " shape='hvh'\n", " )\n", ")\n", "trace5 = go.Scatter(\n", " x=[1, 2, 3, 4, 5],\n", " y=[21, 23, 22, 23, 21],\n", " mode='lines+markers',\n", " name=\"'vh'\",\n", " hoverinfo='name',\n", " line=dict(\n", " shape='vh'\n", " )\n", ")\n", "trace6 = go.Scatter(\n", " x=[1, 2, 3, 4, 5],\n", " y=[26, 28, 27, 28, 26],\n", " mode='lines+markers',\n", " name=\"'hv'\",\n", " hoverinfo='name',\n", " line=dict(\n", " shape='hv'\n", " )\n", ")\n", "data = [trace1, trace2, trace3, trace4, trace5, trace6]\n", "layout = dict(\n", " legend=dict(\n", " y=0.5,\n", " traceorder='reversed',\n", " font=dict(\n", " size=16\n", " )\n", " )\n", ")\n", "fig = dict(data=data, layout=layout)\n", "py.iplot(fig, filename='line-shapes')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Label Lines with Annotations" ] }, { "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", "\n", "title = 'Main Source for News'\n", "\n", "labels = ['Television', 'Newspaper', 'Internet', 'Radio']\n", "\n", "colors = ['rgb(67,67,67)', 'rgb(115,115,115)', 'rgb(49,130,189)', 'rgb(189,189,189)']\n", "\n", "mode_size = [8, 8, 12, 8]\n", "\n", "line_size = [2, 2, 4, 2]\n", "\n", "x_data = [\n", " [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],\n", " [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],\n", " [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],\n", " [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],\n", "]\n", "\n", "y_data = [\n", " [74, 82, 80, 74, 73, 72, 74, 70, 70, 66, 66, 69],\n", " [45, 42, 50, 46, 36, 36, 34, 35, 32, 31, 31, 28],\n", " [13, 14, 20, 24, 20, 24, 24, 40, 35, 41, 43, 50],\n", " [18, 21, 18, 21, 16, 14, 13, 18, 17, 16, 19, 23],\n", "]\n", "\n", "traces = []\n", "\n", "for i in range(0, 4):\n", " traces.append(go.Scatter(\n", " x=x_data[i],\n", " y=y_data[i],\n", " mode='lines',\n", " line=dict(color=colors[i], width=line_size[i]),\n", " connectgaps=True,\n", " ))\n", "\n", " traces.append(go.Scatter(\n", " x=[x_data[i][0], x_data[i][11]],\n", " y=[y_data[i][0], y_data[i][11]],\n", " mode='markers',\n", " marker=dict(color=colors[i], size=mode_size[i])\n", " ))\n", "\n", "layout = go.Layout(\n", " xaxis=dict(\n", " showline=True,\n", " showgrid=False,\n", " showticklabels=True,\n", " linecolor='rgb(204, 204, 204)',\n", " linewidth=2,\n", " ticks='outside',\n", " tickcolor='rgb(204, 204, 204)',\n", " tickwidth=2,\n", " ticklen=5,\n", " tickfont=dict(\n", " family='Arial',\n", " size=12,\n", " color='rgb(82, 82, 82)',\n", " ),\n", " ),\n", " yaxis=dict(\n", " showgrid=False,\n", " zeroline=False,\n", " showline=False,\n", " showticklabels=False,\n", " ),\n", " autosize=False,\n", " margin=dict(\n", " autoexpand=False,\n", " l=100,\n", " r=20,\n", " t=110,\n", " ),\n", " showlegend=False\n", ")\n", "\n", "annotations = []\n", "\n", "# Adding labels\n", "for y_trace, label, color in zip(y_data, labels, colors):\n", " # labeling the left_side of the plot\n", " annotations.append(dict(xref='paper', x=0.05, y=y_trace[0],\n", " xanchor='right', yanchor='middle',\n", " text=label + ' {}%'.format(y_trace[0]),\n", " font=dict(family='Arial',\n", " size=16),\n", " showarrow=False))\n", " # labeling the right_side of the plot\n", " annotations.append(dict(xref='paper', x=0.95, y=y_trace[11],\n", " xanchor='left', yanchor='middle',\n", " text='{}%'.format(y_trace[11]),\n", " font=dict(family='Arial',\n", " size=16),\n", " showarrow=False))\n", "# Title\n", "annotations.append(dict(xref='paper', yref='paper', x=0.0, y=1.05,\n", " xanchor='left', yanchor='bottom',\n", " text='Main Source for News',\n", " font=dict(family='Arial',\n", " size=30,\n", " color='rgb(37,37,37)'),\n", " showarrow=False))\n", "# Source\n", "annotations.append(dict(xref='paper', yref='paper', x=0.5, y=-0.1,\n", " xanchor='center', yanchor='top',\n", " text='Source: PewResearch Center & ' +\n", " 'Storytelling with data',\n", " font=dict(family='Arial',\n", " size=12,\n", " color='rgb(150,150,150)'),\n", " showarrow=False))\n", "\n", "layout['annotations'] = annotations\n", "\n", "fig = go.Figure(data=traces, layout=layout)\n", "py.iplot(fig, filename='news-source')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Filled Lines" ] }, { "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", "\n", "x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "x_rev = x[::-1]\n", "\n", "# Line 1\n", "y1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "y1_upper = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n", "y1_lower = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "y1_lower = y1_lower[::-1]\n", "\n", "# Line 2\n", "y2 = [5, 2.5, 5, 7.5, 5, 2.5, 7.5, 4.5, 5.5, 5]\n", "y2_upper = [5.5, 3, 5.5, 8, 6, 3, 8, 5, 6, 5.5]\n", "y2_lower = [4.5, 2, 4.4, 7, 4, 2, 7, 4, 5, 4.75]\n", "y2_lower = y2_lower[::-1]\n", "\n", "# Line 3\n", "y3 = [10, 8, 6, 4, 2, 0, 2, 4, 2, 0]\n", "y3_upper = [11, 9, 7, 5, 3, 1, 3, 5, 3, 1]\n", "y3_lower = [9, 7, 5, 3, 1, -.5, 1, 3, 1, -1]\n", "y3_lower = y3_lower[::-1]\n", "\n", "trace1 = go.Scatter(\n", " x=x+x_rev,\n", " y=y1_upper+y1_lower,\n", " fill='tozerox',\n", " fillcolor='rgba(0,100,80,0.2)',\n", " line=dict(color='rgba(255,255,255,0)'),\n", " showlegend=False,\n", " name='Fair',\n", ")\n", "trace2 = go.Scatter(\n", " x=x+x_rev,\n", " y=y2_upper+y2_lower,\n", " fill='tozerox',\n", " fillcolor='rgba(0,176,246,0.2)',\n", " line=dict(color='rgba(255,255,255,0)'),\n", " name='Premium',\n", " showlegend=False,\n", ")\n", "trace3 = go.Scatter(\n", " x=x+x_rev,\n", " y=y3_upper+y3_lower,\n", " fill='tozerox',\n", " fillcolor='rgba(231,107,243,0.2)',\n", " line=dict(color='rgba(255,255,255,0)'),\n", " showlegend=False,\n", " name='Fair',\n", ")\n", "trace4 = go.Scatter(\n", " x=x,\n", " y=y1,\n", " line=dict(color='rgb(0,100,80)'),\n", " mode='lines',\n", " name='Fair',\n", ")\n", "trace5 = go.Scatter(\n", " x=x,\n", " y=y2,\n", " line=dict(color='rgb(0,176,246)'),\n", " mode='lines',\n", " name='Premium',\n", ")\n", "trace6 = go.Scatter(\n", " x=x,\n", " y=y3,\n", " line=dict(color='rgb(231,107,243)'),\n", " mode='lines',\n", " name='Ideal',\n", ")\n", "\n", "data = [trace1, trace2, trace3, trace4, trace5, trace6]\n", "\n", "layout = go.Layout(\n", " paper_bgcolor='rgb(255,255,255)',\n", " plot_bgcolor='rgb(229,229,229)',\n", " xaxis=dict(\n", " gridcolor='rgb(255,255,255)',\n", " range=[1,10],\n", " showgrid=True,\n", " showline=False,\n", " showticklabels=True,\n", " tickcolor='rgb(127,127,127)',\n", " ticks='outside',\n", " zeroline=False\n", " ),\n", " yaxis=dict(\n", " gridcolor='rgb(255,255,255)',\n", " showgrid=True,\n", " showline=False,\n", " showticklabels=True,\n", " tickcolor='rgb(127,127,127)',\n", " ticks='outside',\n", " zeroline=False\n", " ),\n", ")\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename= 'shaded_lines')" ] }, { "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-lineplot) can easily be deployed to a PaaS.\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-lineplot/\", width=\"100%\", height=\"650px\", frameBorder=\"0\")\n" ] }, { "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-lineplot/code\", width=\"100%\", height=500, frameBorder=\"0\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#scatter for more information and chart attribute options!" ] }, { "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" } ], "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", " 'lines.ipynb', 'python/line-charts/', 'Python Line Charts',\n", " 'How to make line charts in Python with Plotly. '\n", " 'Examples on creating and styling line charts in Python with Plotly.',\n", " title = 'Python Line Charts | plotly',\n", " name = 'Line Charts',\n", " thumbnail='thumbnail/line-plot.jpg', language='python',\n", " page_type='example_index', has_thumbnail='true', display_as='basic', order=3.3,\n", " ipynb= '~notebook_demo/3')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }