#!/usr/bin/env python # coding: utf-8 # #### New to Plotly? # 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/). #
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). #
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started! # # #### Version Check # Note: Pie Charts are available in version 1.9.12+
# Run `pip install plotly --upgrade` to update your Plotly version # # # In[1]: import plotly plotly.__version__ # ### Basic Pie Chart ### # In[2]: import plotly.plotly as py import plotly.graph_objs as go labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'] values = [4500,2500,1053,500] trace = go.Pie(labels=labels, values=values) py.iplot([trace], filename='basic_pie_chart') # ### Styled Pie Chart # In[3]: import plotly.plotly as py import plotly.graph_objs as go labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'] values = [4500,2500,1053,500] colors = ['#FEBFB3', '#E1396C', '#96D38C', '#D0F9B1'] trace = go.Pie(labels=labels, values=values, hoverinfo='label+percent', textinfo='value', textfont=dict(size=20), marker=dict(colors=colors, line=dict(color='#000000', width=2))) py.iplot([trace], filename='styled_pie_chart') # ### Donut Chart # 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. # In[3]: import plotly.plotly as py import plotly.graph_objs as go fig = { "data": [ { "values": [16, 15, 12, 6, 5, 4, 42], "labels": [ "US", "China", "European Union", "Russian Federation", "Brazil", "India", "Rest of World" ], "domain": {"column": 0}, "name": "GHG Emissions", "hoverinfo":"label+percent+name", "hole": .4, "type": "pie" }, { "values": [27, 11, 25, 8, 1, 3, 25], "labels": [ "US", "China", "European Union", "Russian Federation", "Brazil", "India", "Rest of World" ], "text":["CO2"], "textposition":"inside", "domain": {"column": 1}, "name": "CO2 Emissions", "hoverinfo":"label+percent+name", "hole": .4, "type": "pie" }], "layout": { "title":"Global Emissions 1990-2011", "grid": {"rows": 1, "columns": 2}, "annotations": [ { "font": { "size": 20 }, "showarrow": False, "text": "GHG", "x": 0.20, "y": 0.5 }, { "font": { "size": 20 }, "showarrow": False, "text": "CO2", "x": 0.8, "y": 0.5 } ] } } py.iplot(fig, filename='donut') # ### Pie Chart Subplots ### # 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. # In[5]: import plotly.plotly as py import plotly.graph_objs as go fig = { 'data': [ { 'labels': ['1st', '2nd', '3rd', '4th', '5th'], 'values': [38, 27, 18, 10, 7], 'type': 'pie', 'name': 'Starry Night', 'marker': {'colors': ['rgb(56, 75, 126)', 'rgb(18, 36, 37)', 'rgb(34, 53, 101)', 'rgb(36, 55, 57)', 'rgb(6, 4, 4)']}, 'domain': {'x': [0, .48], 'y': [0, .49]}, 'hoverinfo':'label+percent+name', 'textinfo':'none' }, { 'labels': ['1st', '2nd', '3rd', '4th', '5th'], 'values': [28, 26, 21, 15, 10], 'marker': {'colors': ['rgb(177, 127, 38)', 'rgb(205, 152, 36)', 'rgb(99, 79, 37)', 'rgb(129, 180, 179)', 'rgb(124, 103, 37)']}, 'type': 'pie', 'name': 'Sunflowers', 'domain': {'x': [.52, 1], 'y': [0, .49]}, 'hoverinfo':'label+percent+name', 'textinfo':'none' }, { 'labels': ['1st', '2nd', '3rd', '4th', '5th'], 'values': [38, 19, 16, 14, 13], 'marker': {'colors': ['rgb(33, 75, 99)', 'rgb(79, 129, 102)', 'rgb(151, 179, 100)', 'rgb(175, 49, 35)', 'rgb(36, 73, 147)']}, 'type': 'pie', 'name': 'Irises', 'domain': {'x': [0, .48], 'y': [.51, 1]}, 'hoverinfo':'label+percent+name', 'textinfo':'none' }, { 'labels': ['1st', '2nd', '3rd', '4th', '5th'], 'values': [31, 24, 19, 18, 8], 'marker': {'colors': ['rgb(146, 123, 21)', 'rgb(177, 180, 34)', 'rgb(206, 206, 40)', 'rgb(175, 51, 21)', 'rgb(35, 36, 21)']}, 'type': 'pie', 'name':'The Night Café', 'domain': {'x': [.52, 1], 'y': [.51, 1]}, 'hoverinfo':'label+percent+name', 'textinfo':'none' } ], 'layout': {'title': 'Van Gogh: 5 Most Prominent Colors Shown Proportionally', 'showlegend': False} } py.iplot(fig, filename='pie_chart_subplots') # ### Dash Example # [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. # In[1]: from IPython.display import IFrame IFrame(src= "https://dash-simple-apps.plotly.host/dash-pieplot", width="100%", height="650px" ,frameBorder="0") # In[1]: from IPython.display import IFrame IFrame(src= "https://dash-simple-apps.plotly.host/dash-pieplot/code", width="100%", height=500 ,frameBorder="0") # #### Reference # See https://plotly.com/python/reference/#pie for more information and chart attribute options! # In[3]: from IPython.display import display, HTML display(HTML('')) display(HTML('')) get_ipython().system(' pip install git+https://github.com/plotly/publisher.git --upgrade') import publisher publisher.publish( 'pie-charts.ipynb', 'python/pie-charts/', 'Pie Charts', 'How to make Pie Charts.', title= 'Pie Charts in Python | plotly', has_thumbnail='true', thumbnail='thumbnail/pie-chart.jpg', language='python', page_type='example_index', display_as='basic', order=6, ipynb='~notebook_demo/7/')