{ "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 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": [ "#### Legacy Plots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looking for the old polar chart docs? See [legacy polar charts](https://plotly.com/python/legacy-polar-chart/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic Polar Chart" ] }, { "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.Scatterpolar(\n", " r = [0.5,1,2,2.5,3,4],\n", " theta = [35,70,120,155,205,240],\n", " mode = 'markers',\n", " marker = dict(\n", " color = 'peru'\n", " )\n", " )\n", "]\n", "\n", "layout = go.Layout(\n", " showlegend = False\n", ")\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename = 'polar-basic')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Line Polar Chart" ] }, { "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", "import pandas as pd\n", "\n", "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/polar_dataset.csv\")\n", "\n", "data = [\n", " go.Scatterpolar(\n", " r = df['x1'],\n", " theta = df['y'],\n", " mode = 'lines',\n", " name = 'Figure8',\n", " line = dict(\n", " color = 'peru'\n", " ) \n", " ),\n", " go.Scatterpolar(\n", " r = df['x2'],\n", " theta = df['y'],\n", " mode = 'lines',\n", " name = 'Cardioid',\n", " line = dict(\n", " color = 'darkviolet'\n", " ) \n", " ),\n", " go.Scatterpolar(\n", " r = df['x3'],\n", " theta = df['y'],\n", " mode = 'lines',\n", " name = 'Hypercardioid',\n", " line = dict(\n", " color = 'deepskyblue'\n", " ) \n", " ),\n", " go.Scatterpolar(\n", " r = df['x4'],\n", " theta = df['y'],\n", " mode = 'lines',\n", " name = 'orangered',\n", " line = dict(\n", " color = 'orangered'\n", " ) \n", " ),\n", " go.Scatterpolar(\n", " r = df['x5'],\n", " theta = df['y'],\n", " mode = 'lines',\n", " name = 'Supercardioid',\n", " line = dict(\n", " color = 'green'\n", " ) \n", " )\n", "]\n", "\n", "layout = go.Layout(\n", " title = 'Mic Patterns',\n", " font = dict(\n", " family = 'Arial, sans-serif;',\n", " size = 12,\n", " color = '#000'\n", " ),\n", " showlegend = False\n", ")\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename = 'polar-line')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Area Polar Chart" ] }, { "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.Scatterpolar(\n", " r = [0, 1.5, 1.5, 0, 2.5, 2.5, 0],\n", " theta = [0, 10, 25, 0, 205, 215, 0],\n", " mode = 'lines',\n", " fill = 'toself',\n", " fillcolor = '#709BFF',\n", " line = dict(\n", " color = 'black'\n", " ) \n", " ),\n", " go.Scatterpolar(\n", " r = [0, 3.5, 3.5, 0],\n", " theta = [0, 55, 75, 0],\n", " mode = 'lines',\n", " fill = 'toself',\n", " fillcolor = '#E4FF87',\n", " line = dict(\n", " color = 'black'\n", " ) \n", " ),\n", " go.Scatterpolar(\n", " r = [0, 4.5, 4.5, 0, 4.5, 4.5, 0],\n", " theta = [0, 100, 120, 0, 305, 320, 0],\n", " mode = 'lines',\n", " fill = 'toself',\n", " fillcolor = '#FFAA70',\n", " line = dict(\n", " color = 'black'\n", " ) \n", " ),\n", " go.Scatterpolar(\n", " r = [0, 4, 4, 0],\n", " theta = [0, 165, 195, 0],\n", " mode = 'lines',\n", " fill = 'toself',\n", " fillcolor = '#FFDF70',\n", " line = dict(\n", " color = 'black'\n", " ) \n", " ),\n", " go.Scatterpolar(\n", " r = [0, 3, 3, 0],\n", " theta = [0, 262.5, 277.5, 0],\n", " mode = 'lines',\n", " fill = 'toself',\n", " fillcolor = '#B6FFB4',\n", " line = dict(\n", " color = 'black'\n", " ) \n", " )\n", "]\n", "\n", "layout = go.Layout(\n", " polar = dict(\n", " radialaxis = dict(\n", " visible = True,\n", " range = [0,5]\n", " )\n", " ),\n", " showlegend = False\n", ")\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename = 'polar-area')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Categorical Polar Chart" ] }, { "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", "\n", "data = [\n", " go.Scatterpolar(\n", " name = \"angular categories\",\n", " r = [5, 4, 2, 4, 5],\n", " theta = [\"a\", \"b\", \"c\", \"d\", \"a\"],\n", " fill = \"toself\"\n", " ),\n", " go.Scatterpolar(\n", " name = \"radial categories\",\n", " r = [\"a\", \"b\", \"c\", \"d\", \"b\", \"f\", \"a\"],\n", " theta = [1, 4, 2, 1.5, 1.5, 6, 5],\n", " thetaunit = \"radians\",\n", " fill = \"toself\",\n", " subplot = \"polar2\"\n", " ),\n", " go.Scatterpolar(\n", " name = \"angular categories (w/ categoryarray)\",\n", " r = [5, 4, 2, 4, 5],\n", " theta = [\"a\", \"b\", \"c\", \"d\", \"a\"],\n", " fill = \"toself\",\n", " subplot = \"polar3\"\n", " ),\n", " go.Scatterpolar(\n", " name = \"radial categories (w/ category descending)\",\n", " r = [\"a\", \"b\", \"c\", \"d\", \"b\", \"f\", \"a\", \"a\"],\n", " theta = [45, 90, 180, 200, 300, 15, 20, 45],\n", " fill = \"toself\",\n", " subplot = \"polar4\"\n", " ),\n", " go.Scatterpolar(\n", " name = \"angular categories (w/ extra category)\",\n", " r = [5, 4, 2, 4, 5, 5],\n", " theta = [\"b\", \"c\", \"d\", \"e\", \"a\", \"b\"],\n", " fill = \"toself\"\n", " )\n", " ]\n", " \n", " \n", "layout = go.Layout(\n", " polar = dict(\n", " domain = dict(\n", " x = [0, 0.46],\n", " y = [0.56, 1]\n", " ),\n", " radialaxis = dict(\n", " angle = 45\n", " ),\n", " angularaxis = dict(\n", " direction = \"clockwise\",\n", " period = 6\n", " )\n", " ),\n", " polar2 = dict(\n", " domain = dict(\n", " x = [0, 0.46],\n", " y = [0, 0.44]\n", " ),\n", " radialaxis = dict(\n", " angle = 180,\n", " tickangle = -180\n", " )\n", " ),\n", " polar3 = dict(\n", " domain = dict(\n", " x = [0.54, 1],\n", " y = [0.56, 1]\n", " ),\n", " sector = [150, 400],\n", " radialaxis = dict(\n", " angle = -45\n", " ),\n", " angularaxis = dict(\n", " categoryarray = [\"d\", \"a\", \"c\", \"b\"]\n", " )\n", " ),\n", " polar4 = dict(\n", " domain = dict(\n", " x = [0.54, 1],\n", " y = [0, 0.44]\n", " ),\n", " radialaxis = dict(\n", " categoryorder = \"category descending\"\n", " ),\n", " angularaxis = dict(\n", " thetaunit = \"radians\",\n", " dtick = 0.3141592653589793\n", " )\n", " )\n", ")\n", "\n", "fig = go.Figure(data=data,layout=layout)\n", "py.iplot(fig, filename='polar-category')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Polar Chart Sector" ] }, { "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", "\n", "\n", "data = [\n", " go.Scatterpolar(\n", " mode = \"lines+markers\",\n", " r = [1,2,3,4,5],\n", " theta = [0,90,180,360,0],\n", " line = dict(\n", " color = \"#ff66ab\"\n", " ),\n", " marker = dict(\n", " color = \"#8090c7\",\n", " symbol = \"square\",\n", " size = 8\n", " ),\n", " subplot = \"polar\",\n", " ),\n", " go.Scatterpolar(\n", " mode = \"lines+markers\",\n", " r = [1,2,3,4,5],\n", " theta = [0,90,180,360,0],\n", " line = dict(\n", " color = \"#ff66ab\"\n", " ),\n", " marker = dict(\n", " color = \"#8090c7\",\n", " symbol = \"square\",\n", " size = 8\n", " ),\n", " subplot = \"polar2\"\n", " )\n", " ]\n", "\n", "\n", "layout = go.Layout(\n", " showlegend = False,\n", " polar = dict(\n", " domain = dict(\n", " x = [0,0.4],\n", " y = [0,1]\n", " ),\n", " sector = [150,210],\n", " radialaxis = dict(\n", " tickfont = dict(\n", " size = 8\n", " )\n", " ),\n", " angularaxis = dict(\n", " tickfont = dict(\n", " size = 8\n", " )\n", " )\n", " ),\n", " polar2 = dict(\n", " domain = dict(\n", " x = [0.6,1],\n", " y = [0,1]\n", " ),\n", " radialaxis = dict(\n", " tickfont = dict(\n", " size = 8\n", " )\n", " ),\n", " angularaxis = dict(\n", " tickfont = dict(\n", " size = 8\n", " )\n", " )\n", " )\n", ")\n", "\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='polar-sector')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Polar Chart Directions" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "data = [\n", " go.Scatterpolar(\n", " mode = \"lines+markers\",\n", " r = [1,2,3,4,5],\n", " theta = [0,90,180,360,0],\n", " line = dict(\n", " color = \"#ff66ab\"\n", " ),\n", " marker = dict(\n", " color = \"#8090c7\",\n", " symbol = \"square\",\n", " size = 8\n", " ),\n", " subplot = \"polar\",\n", " ),\n", " go.Scatterpolar(\n", " mode = \"lines+markers\",\n", " r = [1,2,3,4,5],\n", " theta = [0,90,180,360,0],\n", " line = dict(\n", " color = \"#ff66ab\"\n", " ),\n", " marker = dict(\n", " color = \"#8090c7\",\n", " symbol = \"square\",\n", " size = 8\n", " ),\n", " subplot = \"polar2\"\n", " )\n", " ]\n", "\n", "\n", "layout = go.Layout(\n", " showlegend = False,\n", " polar = dict(\n", " domain = dict(\n", " x = [0,0.4],\n", " y = [0,1]\n", " ),\n", " radialaxis = dict(\n", " tickfont = dict(\n", " size = 8\n", " )\n", " ),\n", " angularaxis = dict(\n", " tickfont = dict(\n", " size = 8\n", " ),\n", " rotation = 90,\n", " direction = \"counterclockwise\"\n", " )\n", " ),\n", " polar2 = dict(\n", " domain = dict(\n", " x = [0.6,1],\n", " y = [0,1]\n", " ),\n", " radialaxis = dict(\n", " tickfont = dict(\n", " size = 8\n", " )\n", " ),\n", " angularaxis = dict(\n", " tickfont = dict(\n", " size = 8\n", " ),\n", " rotation = 90,\n", " direction = \"clockwise\"\n", " ),\n", " )\n", ")\n", "\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='polar-directions')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Webgl Polar Chart" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 17, "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/hobbs-pearson-trials.csv\")\n", "\n", "\n", "data = [\n", " go.Scatterpolargl(\n", " r = df.trial_1_r,\n", " theta = df.trial_1_theta,\n", " mode = \"markers\",\n", " name = \"Trial 1\",\n", " marker = dict(\n", " color = \"rgb(27,158,119)\",\n", " size = 15,\n", " line = dict(\n", " color = \"white\"\n", " ),\n", " opacity = 0.7\n", " )\n", " ),\n", " go.Scatterpolargl(\n", " r = df.trial_2_r,\n", " theta = df.trial_2_theta,\n", " mode = \"markers\",\n", " name = \"Trial 2\",\n", " marker = dict(\n", " color = \"rgb(217,95,2)\",\n", " size = 20,\n", " line = dict(\n", " color = \"white\"\n", " ),\n", " opacity = 0.7\n", " )\n", " ),\n", " go.Scatterpolargl(\n", " r = df.trial_3_r,\n", " theta = df.trial_3_theta,\n", " mode = \"markers\",\n", " name = \"Trial 3\",\n", " marker = dict(\n", " color = \"rgb(117,112,179)\",\n", " size = 12,\n", " line = dict(\n", " color = \"white\"\n", " ),\n", " opacity = 0.7\n", " )\n", " ),\n", " go.Scatterpolargl(\n", " r = df.trial_4_r,\n", " theta = df.trial_4_theta,\n", " mode = \"markers\",\n", " name = \"Trial 4\",\n", " marker = dict(\n", " color = \"rgb(231,41,138)\",\n", " size = 22,\n", " line = dict(\n", " color = \"white\"\n", " ),\n", " opacity = 0.7\n", " )\n", " ),\n", " go.Scatterpolargl(\n", " r = df.trial_5_r,\n", " theta = df.trial_5_theta,\n", " mode = \"markers\",\n", " name = \"Trial 5\",\n", " marker = dict(\n", " color = \"rgb(102,166,30)\",\n", " size = 19,\n", " line = dict(\n", " color = \"white\"\n", " ),\n", " opacity = 0.7\n", " )\n", " ),\n", " go.Scatterpolargl(\n", " r = df.trial_6_r,\n", " theta = df.trial_6_theta,\n", " mode = \"markers\",\n", " name = \"Trial 6\",\n", " marker = dict(\n", " color = \"rgb(230,171,2)\",\n", " size = 10,\n", " line = dict(\n", " color = \"white\"\n", " ),\n", " opacity = 0.7\n", " )\n", " )\n", "]\n", " \n", "layout = go.Layout(\n", " title = \"Hobbs-Pearson Trials\",\n", " font = dict(\n", " size = 15\n", " ),\n", " showlegend = False,\n", " polar = dict(\n", " bgcolor = \"rgb(223, 223, 223)\",\n", " angularaxis = dict(\n", " tickwidth = 2,\n", " linewidth = 3,\n", " layer = \"below traces\"\n", " ),\n", " radialaxis = dict(\n", " side = \"counterclockwise\",\n", " showline = True,\n", " linewidth = 2,\n", " tickwidth = 2,\n", " gridcolor = \"white\",\n", " gridwidth = 2\n", " )\n", " ),\n", " paper_bgcolor = \"rgb(223, 223, 223)\"\n", ")\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='polar-webgl')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Polar Chart Subplots" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "data = [\n", " go.Scatterpolar(\n", " r = [1, 2, 3],\n", " theta = [50, 100, 200],\n", " marker = dict(symbol = \"square\")\n", " ), \n", " go.Scatterpolar(\n", " r = [1, 2, 3],\n", " theta = [1, 2, 3],\n", " thetaunit = \"radians\"\n", " ), \n", " go.Scatterpolar(\n", " r = [\"a\", \"b\", \"c\", \"b\"],\n", " theta = [\"D\", \"C\", \"B\", \"A\"],\n", " subplot = \"polar2\"\n", " ), \n", " go.Scatterpolar(\n", " r = [50, 300, 900],\n", " theta = [0, 90, 180],\n", " subplot = \"polar3\"\n", " ), \n", " go.Scatterpolar(\n", " mode = \"lines\",\n", " r = [3, 3, 4, 3],\n", " theta = [0, 45, 90, 270],\n", " fill = \"toself\",\n", " subplot = \"polar4\"\n", " )\n", "]\n", " \n", "layout = go.Layout(\n", " polar = dict(\n", " domain = dict(\n", " x = [0, 0.46],\n", " y = [0.56, 1]\n", " ),\n", " radialaxis = dict(\n", " range = [1, 4]\n", " ),\n", " angularaxis = dict(\n", " thetaunit = \"radians\"\n", " )\n", " ),\n", " polar2 =dict(\n", " domain = dict(\n", " x = [0, 0.46],\n", " y = [0, 0.42]\n", " )\n", " ),\n", " polar3 = dict(\n", " domain = dict(\n", " x = [0.54, 1],\n", " y = [0.56, 1]\n", " ),\n", " radialaxis = dict(\n", " type = \"log\",\n", " tickangle = 45\n", " ),\n", " sector = [0, 180]\n", " ),\n", " polar4 = dict(\n", " domain = dict(\n", " x = [0.54, 1],\n", " y = [0, 0.44]\n", " ),\n", " radialaxis = dict(\n", " visible = False,\n", " range = [0, 6]\n", " )\n", " ),\n", " showlegend = False\n", ")\n", "\n", "fig = go.Figure(data=data,layout=layout)\n", "py.iplot(fig, filename='polar-subplot')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#scatterpolar for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 19, "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 c:\\users\\brand\\appdata\\local\\temp\\pip-req-build-f59lckmf\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", " Running setup.py install for publisher: started\n", " Running setup.py install for publisher: finished with status 'done'\n", "Successfully installed publisher-0.11\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Python27\\lib\\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", "C:\\Python27\\lib\\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", " 'polar.ipynb', 'python/polar-chart/', 'Polar Charts',\n", " 'How to makepolar charts in Python with Plotly.',\n", " title = 'Polar Charts | Plotly',\n", " has_thumbnail='true', thumbnail='thumbnail/polar.gif', \n", " language='python', \n", " # page_type='example_index', // note this is only if you want the tutorial to appear on the main page: plot.ly/python\n", " display_as='scientific', order=29, ipynb='~notebook_demo/200',\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.14" } }, "nbformat": 4, "nbformat_minor": 2 }