{ "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": [ "'2.4.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic Dot Plot\n", "Dot plots show changes between two points in time or between two conditions. " ] }, { "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", "trace1 = {\"x\": [72, 67, 73, 80, 76, 79, 84, 78, 86, 93, 94, 90, 92, 96, 94, 112], \n", " \"y\": [\"Brown\", \"NYU\", \"Notre Dame\", \"Cornell\", \"Tufts\", \"Yale\",\n", " \"Dartmouth\", \"Chicago\", \"Columbia\", \"Duke\", \"Georgetown\",\n", " \"Princeton\", \"U.Penn\", \"Stanford\", \"MIT\", \"Harvard\"], \n", " \"marker\": {\"color\": \"pink\", \"size\": 12}, \n", " \"mode\": \"markers\", \n", " \"name\": \"Women\", \n", " \"type\": \"scatter\"\n", "}\n", "\n", "trace2 = {\"x\": [92, 94, 100, 107, 112, 114, 114, 118, 119, 124, 131, 137, 141, 151, 152, 165], \n", " \"y\": [\"Brown\", \"NYU\", \"Notre Dame\", \"Cornell\", \"Tufts\", \"Yale\",\n", " \"Dartmouth\", \"Chicago\", \"Columbia\", \"Duke\", \"Georgetown\",\n", " \"Princeton\", \"U.Penn\", \"Stanford\", \"MIT\", \"Harvard\"], \n", " \"marker\": {\"color\": \"blue\", \"size\": 12}, \n", " \"mode\": \"markers\", \n", " \"name\": \"Men\", \n", " \"type\": \"scatter\", \n", "}\n", "\n", "data = [trace1, trace2]\n", "layout = {\"title\": \"Gender Earnings Disparity\", \n", " \"xaxis\": {\"title\": \"Annual Salary (in thousands)\", }, \n", " \"yaxis\": {\"title\": \"School\"}}\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filenmae='basic_dot-plot')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Styled Categorical Dot Plot" ] }, { "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", "country = ['Switzerland (2011)', 'Chile (2013)', 'Japan (2014)',\n", " 'United States (2012)', 'Slovenia (2014)', 'Canada (2011)',\n", " 'Poland (2010)', 'Estonia (2015)', 'Luxembourg (2013)', 'Portugal (2011)']\n", "voting_pop = [40, 45.7, 52, 53.6, 54.1, 54.2, 54.5, 54.7, 55.1, 56.6]\n", "reg_voters = [49.1, 42, 52.7, 84.3, 51.7, 61.1, 55.3, 64.2, 91.1, 58.9]\n", "\n", "trace0 = go.Scatter(\n", " x=voting_pop,\n", " y=country,\n", " mode='markers',\n", " name='Percent of estimated voting age population',\n", " marker=dict(\n", " color='rgba(156, 165, 196, 0.95)',\n", " line=dict(\n", " color='rgba(156, 165, 196, 1.0)',\n", " width=1,\n", " ),\n", " symbol='circle',\n", " size=16,\n", " )\n", ")\n", "trace1 = go.Scatter(\n", " x=reg_voters,\n", " y=country,\n", " mode='markers',\n", " name='Percent of estimated registered voters',\n", " marker=dict(\n", " color='rgba(204, 204, 204, 0.95)',\n", " line=dict(\n", " color='rgba(217, 217, 217, 1.0)',\n", " width=1,\n", " ),\n", " symbol='circle',\n", " size=16,\n", " )\n", ")\n", "\n", "data = [trace0, trace1]\n", "layout = go.Layout(\n", " title=\"Votes cast for ten lowest voting age population in OECD countries\",\n", " xaxis=dict(\n", " showgrid=False,\n", " showline=True,\n", " linecolor='rgb(102, 102, 102)',\n", " titlefont=dict(\n", " color='rgb(204, 204, 204)'\n", " ),\n", " tickfont=dict(\n", " color='rgb(102, 102, 102)',\n", " ),\n", " showticklabels=True,\n", " dtick=10,\n", " ticks='outside',\n", " tickcolor='rgb(102, 102, 102)',\n", " ),\n", " margin=dict(\n", " l=140,\n", " r=40,\n", " b=50,\n", " t=80\n", " ),\n", " legend=dict(\n", " font=dict(\n", " size=10,\n", " ),\n", " yanchor='middle',\n", " xanchor='right',\n", " ),\n", " width=800,\n", " height=600,\n", " paper_bgcolor='rgb(254, 247, 234)',\n", " plot_bgcolor='rgb(254, 247, 234)',\n", " hovermode='closest',\n", ")\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='lowest-oecd-votes-cast')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See https://plotly.com/python/reference/#scatter for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": false }, "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-ttsIdw\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-t4Sl31/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", " \n", "import publisher\n", "publisher.publish(\n", " 'dot.ipynb', 'python/dot-plots/', 'Dot Plots',\n", " 'How to make dot plots in Python with Plotly.',\n", " title = 'Python Dot Plots | plotly',\n", " has_thumbnail='true', thumbnail='thumbnail/dot-plot.jpg', \n", " language='python',\n", " display_as='basic', order=3.1,\n", " ipynb= '~notebook_demo/2')" ] }, { "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 }