{ "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": [ "#### Basic Symmetric Error Bars" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "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", "data = [\n", " go.Scatter(\n", " x=[0, 1, 2],\n", " y=[6, 10, 2],\n", " error_y=dict(\n", " type='data',\n", " array=[1, 2, 3],\n", " visible=True\n", " )\n", " )\n", "]\n", "\n", "py.iplot(data, filename='basic-error-bar')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Asymmetric Error Bars" ] }, { "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", "data = [\n", " go.Scatter(\n", " x=[1, 2, 3, 4],\n", " y=[2, 1, 3, 4],\n", " error_y=dict(\n", " type='data',\n", " symmetric=False,\n", " array=[0.1, 0.2, 0.1, 0.1],\n", " arrayminus=[0.2, 0.4, 1, 0.2]\n", " )\n", " )\n", "]\n", "py.iplot(data, filename='error-bar-asymmetric-array')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Error Bars as a Percentage of the y Value" ] }, { "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", "data = [\n", " go.Scatter(\n", " x=[0, 1, 2],\n", " y=[6, 10, 2],\n", " error_y=dict(\n", " type='percent',\n", " value=50,\n", " visible=True\n", " )\n", " )\n", "]\n", "py.iplot(data, filename='percent-error-bar')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Asymmetric Error Bars with a Constant Offset" ] }, { "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", "data = [\n", " go.Scatter(\n", " x=[1, 2, 3, 4],\n", " y=[2, 1, 3, 4],\n", " error_y=dict(\n", " type='percent',\n", " symmetric=False,\n", " value=15,\n", " valueminus=25\n", " )\n", " )\n", "]\n", "py.iplot(data, filename='error-bar-asymmetric-constant')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Horizontal Error Bars" ] }, { "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", "data = [\n", " go.Scatter(\n", " x=[1, 2, 3, 4],\n", " y=[2, 1, 3, 4],\n", " error_x=dict(\n", " type='percent',\n", " value=10\n", " )\n", " )\n", "]\n", "py.iplot(data, filename='error-bar-horizontal')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Bar Chart with Error Bars" ] }, { "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.Bar(\n", " x=['Trial 1', 'Trial 2', 'Trial 3'],\n", " y=[3, 6, 4],\n", " name='Control',\n", " error_y=dict(\n", " type='data',\n", " array=[1, 0.5, 1.5],\n", " visible=True\n", " )\n", ")\n", "trace2 = go.Bar(\n", " x=['Trial 1', 'Trial 2', 'Trial 3'],\n", " y=[4, 7, 3],\n", " name='Experimental',\n", " error_y=dict(\n", " type='data',\n", " array=[0.5, 1, 2],\n", " visible=True\n", " )\n", ")\n", "data = [trace1, trace2]\n", "layout = go.Layout(\n", " barmode='group'\n", ")\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='error-bar-bar')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Colored and Styled Error Bars" ] }, { "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", "import numpy as np\n", "\n", "x_theo = np.linspace(-4, 4, 100)\n", "sincx = np.sinc(x_theo)\n", "x = [-3.8, -3.03, -1.91, -1.46, -0.89, -0.24, -0.0, 0.41, 0.89, 1.01, 1.91, 2.28, 2.79, 3.56]\n", "y = [-0.02, 0.04, -0.01, -0.27, 0.36, 0.75, 1.03, 0.65, 0.28, 0.02, -0.11, 0.16, 0.04, -0.15]\n", "\n", "trace1 = go.Scatter(\n", " x=x_theo,\n", " y=sincx,\n", " name='sinc(x)'\n", ")\n", "trace2 = go.Scatter(\n", " x=x,\n", " y=y,\n", " mode='markers',\n", " name='measured',\n", " error_y=dict(\n", " type='constant',\n", " value=0.1,\n", " color='#85144B',\n", " thickness=1.5,\n", " width=3,\n", " ),\n", " error_x=dict(\n", " type='constant',\n", " value=0.2,\n", " color='#85144B',\n", " thickness=1.5,\n", " width=3,\n", " ),\n", " marker=dict(\n", " color='#85144B',\n", " size=8\n", " )\n", ")\n", "data = [trace1, trace2]\n", "py.iplot(data, filename='error-bar-style')" ] }, { "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": 2, "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-req-build-FHZDff\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-gwfx1n/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", "Successfully 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", " 'error-bars.ipynb', 'python/error-bars/', 'Error Bars | plotly',\n", " 'How to add error-bars to charts in Python with Plotly.',\n", " title = 'Error Bars | plotly',\n", " name = 'Error Bars',\n", " thumbnail='thumbnail/error-bar.jpg', language='python',\n", " page_type='example_index', has_thumbnail='true', display_as='statistical', order=1,\n", " ipynb='~notebook_demo/18')" ] }, { "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 }