{ "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version Check\n", "Note: Pie Charts are available in version 1.9.12+
\n", "Run `pip install plotly --upgrade` to update your Plotly version\n", "\n" ] }, { "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 Pie 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", "labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']\n", "values = [4500,2500,1053,500]\n", "\n", "trace = go.Pie(labels=labels, values=values)\n", "\n", "py.iplot([trace], filename='basic_pie_chart')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Styled Pie 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", "labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']\n", "values = [4500,2500,1053,500]\n", "colors = ['#FEBFB3', '#E1396C', '#96D38C', '#D0F9B1']\n", "\n", "trace = go.Pie(labels=labels, values=values,\n", " hoverinfo='label+percent', textinfo='value', \n", " textfont=dict(size=20),\n", " marker=dict(colors=colors, \n", " line=dict(color='#000000', width=2)))\n", "\n", "py.iplot([trace], filename='styled_pie_chart')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Donut Chart\n", "This example uses a [plotly grid attribute](https://plotly.com/python/reference/#layout-grid) for the suplots. Reference the row and column destination using the [domain](https://plotly.com/python/reference/#pie-domain) attribute." ] }, { "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", "fig = {\n", " \"data\": [\n", " {\n", " \"values\": [16, 15, 12, 6, 5, 4, 42],\n", " \"labels\": [\n", " \"US\",\n", " \"China\",\n", " \"European Union\",\n", " \"Russian Federation\",\n", " \"Brazil\",\n", " \"India\",\n", " \"Rest of World\"\n", " ],\n", " \"domain\": {\"column\": 0},\n", " \"name\": \"GHG Emissions\",\n", " \"hoverinfo\":\"label+percent+name\",\n", " \"hole\": .4,\n", " \"type\": \"pie\"\n", " },\n", " {\n", " \"values\": [27, 11, 25, 8, 1, 3, 25],\n", " \"labels\": [\n", " \"US\",\n", " \"China\",\n", " \"European Union\",\n", " \"Russian Federation\",\n", " \"Brazil\",\n", " \"India\",\n", " \"Rest of World\"\n", " ],\n", " \"text\":[\"CO2\"],\n", " \"textposition\":\"inside\",\n", " \"domain\": {\"column\": 1},\n", " \"name\": \"CO2 Emissions\",\n", " \"hoverinfo\":\"label+percent+name\",\n", " \"hole\": .4,\n", " \"type\": \"pie\"\n", " }],\n", " \"layout\": {\n", " \"title\":\"Global Emissions 1990-2011\",\n", " \"grid\": {\"rows\": 1, \"columns\": 2},\n", " \"annotations\": [\n", " {\n", " \"font\": {\n", " \"size\": 20\n", " },\n", " \"showarrow\": False,\n", " \"text\": \"GHG\",\n", " \"x\": 0.20,\n", " \"y\": 0.5\n", " },\n", " {\n", " \"font\": {\n", " \"size\": 20\n", " },\n", " \"showarrow\": False,\n", " \"text\": \"CO2\",\n", " \"x\": 0.8,\n", " \"y\": 0.5\n", " }\n", " ]\n", " }\n", "}\n", "py.iplot(fig, filename='donut')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pie Chart Subplots ###" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to create pie chart subplots, you need to use the [domain](https://plotly.com/python/reference/#pie-domain) attribute. It is important to note that the `X` array set the horizontal position whilst the `Y` array sets the vertical. For example, `x: [0,0.5], y: [0, 0.5]` would mean the bottom left position of the plot." ] }, { "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", "fig = {\n", " 'data': [\n", " {\n", " 'labels': ['1st', '2nd', '3rd', '4th', '5th'],\n", " 'values': [38, 27, 18, 10, 7],\n", " 'type': 'pie',\n", " 'name': 'Starry Night',\n", " 'marker': {'colors': ['rgb(56, 75, 126)',\n", " 'rgb(18, 36, 37)',\n", " 'rgb(34, 53, 101)',\n", " 'rgb(36, 55, 57)',\n", " 'rgb(6, 4, 4)']},\n", " 'domain': {'x': [0, .48],\n", " 'y': [0, .49]},\n", " 'hoverinfo':'label+percent+name',\n", " 'textinfo':'none'\n", " },\n", " {\n", " 'labels': ['1st', '2nd', '3rd', '4th', '5th'],\n", " 'values': [28, 26, 21, 15, 10],\n", " 'marker': {'colors': ['rgb(177, 127, 38)',\n", " 'rgb(205, 152, 36)',\n", " 'rgb(99, 79, 37)',\n", " 'rgb(129, 180, 179)',\n", " 'rgb(124, 103, 37)']},\n", " 'type': 'pie',\n", " 'name': 'Sunflowers',\n", " 'domain': {'x': [.52, 1],\n", " 'y': [0, .49]},\n", " 'hoverinfo':'label+percent+name',\n", " 'textinfo':'none'\n", "\n", " },\n", " {\n", " 'labels': ['1st', '2nd', '3rd', '4th', '5th'],\n", " 'values': [38, 19, 16, 14, 13],\n", " 'marker': {'colors': ['rgb(33, 75, 99)',\n", " 'rgb(79, 129, 102)',\n", " 'rgb(151, 179, 100)',\n", " 'rgb(175, 49, 35)',\n", " 'rgb(36, 73, 147)']},\n", " 'type': 'pie',\n", " 'name': 'Irises',\n", " 'domain': {'x': [0, .48],\n", " 'y': [.51, 1]},\n", " 'hoverinfo':'label+percent+name',\n", " 'textinfo':'none'\n", " },\n", " {\n", " 'labels': ['1st', '2nd', '3rd', '4th', '5th'],\n", " 'values': [31, 24, 19, 18, 8],\n", " 'marker': {'colors': ['rgb(146, 123, 21)',\n", " 'rgb(177, 180, 34)',\n", " 'rgb(206, 206, 40)',\n", " 'rgb(175, 51, 21)',\n", " 'rgb(35, 36, 21)']},\n", " 'type': 'pie',\n", " 'name':'The Night Café',\n", " 'domain': {'x': [.52, 1],\n", " 'y': [.51, 1]},\n", " 'hoverinfo':'label+percent+name',\n", " 'textinfo':'none'\n", " }\n", " ],\n", " 'layout': {'title': 'Van Gogh: 5 Most Prominent Colors Shown Proportionally',\n", " 'showlegend': False}\n", "}\n", "\n", "py.iplot(fig, filename='pie_chart_subplots')" ] }, { "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-pieplot) can easily be deployed to a PaaS." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-pieplot\", width=\"100%\", height=\"650px\" ,frameBorder=\"0\")" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-pieplot/code\", width=\"100%\", height=500 ,frameBorder=\"0\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#pie 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" }, { "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/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-g4xc22vb\n", "Building wheels for collected packages: publisher\n", " Building wheel for publisher (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-w09kkn22/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.13\n", " Uninstalling publisher-0.13:\n", " Successfully uninstalled publisher-0.13\n", "Successfully installed publisher-0.13\n", "\u001b[33mYou are using pip version 19.0.3, however version 19.1.1 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\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", " 'pie-charts.ipynb', 'python/pie-charts/', 'Pie Charts',\n", " 'How to make Pie Charts.',\n", " title= 'Pie Charts in Python | plotly',\n", " has_thumbnail='true', thumbnail='thumbnail/pie-chart.jpg', \n", " language='python', page_type='example_index', \n", " display_as='basic', order=6,\n", " ipynb='~notebook_demo/7/')" ] } ], "metadata": { "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": 2 }