{ "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\n", "Plotly's Python API is updated frequently. Run `pip install plotly --upgrade` to update your version." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.4.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic Continuous Error Bars\n" ] }, { "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", "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='Ideal',\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": [ "#### Asymmetric Error Bars with a Constant Offset" ] }, { "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", "import pandas as pd\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/wind_speed_laurel_nebraska.csv')\n", "\n", "upper_bound = go.Scatter(\n", " name='Upper Bound',\n", " x=df['Time'],\n", " y=df['10 Min Sampled Avg']+df['10 Min Std Dev'],\n", " mode='lines',\n", " marker=dict(color=\"#444\"),\n", " line=dict(width=0),\n", " fillcolor='rgba(68, 68, 68, 0.3)',\n", " fill='tonexty')\n", "\n", "trace = go.Scatter(\n", " name='Measurement',\n", " x=df['Time'],\n", " y=df['10 Min Sampled Avg'],\n", " mode='lines',\n", " line=dict(color='rgb(31, 119, 180)'),\n", " fillcolor='rgba(68, 68, 68, 0.3)',\n", " fill='tonexty')\n", "\n", "lower_bound = go.Scatter(\n", " name='Lower Bound',\n", " x=df['Time'],\n", " y=df['10 Min Sampled Avg']-df['10 Min Std Dev'],\n", " marker=dict(color=\"#444\"),\n", " line=dict(width=0),\n", " mode='lines')\n", "\n", "# Trace order can be important\n", "# with continuous error bars\n", "data = [lower_bound, trace, upper_bound]\n", "\n", "layout = go.Layout(\n", " yaxis=dict(title='Wind speed (m/s)'),\n", " title='Continuous, variable value error bars.
Notice the hover text!',\n", " showlegend = False)\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='pandas-continuous-error-bars')" ] }, { "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": "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", " 'cont-error-bars.ipynb', 'python/continuous-error-bars/', 'Continuous Error Bars',\n", " 'Add continuous error bars to charts in Python with Plotly.',\n", " title = 'Continuous Error Bars | plotly',\n", " name = 'Continuous Error Bars',\n", " thumbnail='thumbnail/error-cont.jpg', language='python',\n", " has_thumbnail='true', display_as='statistical', order=2,\n", " ipynb='~notebook_demo/19')" ] }, { "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.12" } }, "nbformat": 4, "nbformat_minor": 1 }