Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!
Note: Pie Charts are available in version 1.9.12+
Run pip install plotly --upgrade
to update your Plotly version
import plotly
plotly.__version__
'2.4.1'
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')
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')
This example uses a plotly grid attribute for the suplots. Reference the row and column destination using the domain attribute.
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')
In order to create pie chart subplots, you need to use the 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.
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 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 can easily be deployed to a PaaS.
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-pieplot", width="100%", height="650px" ,frameBorder="0")
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-pieplot/code", width="100%", height=500 ,frameBorder="0")
See https://plotly.com/python/reference/#pie for more information and chart attribute options!
from IPython.display import display, HTML
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
! 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/')
Collecting git+https://github.com/plotly/publisher.git
Cloning https://github.com/plotly/publisher.git to /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-g4xc22vb
Building wheels for collected packages: publisher
Building wheel for publisher (setup.py) ... done
Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-w09kkn22/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966
Successfully built publisher
Installing collected packages: publisher
Found existing installation: publisher 0.13
Uninstalling publisher-0.13:
Successfully uninstalled publisher-0.13
Successfully installed publisher-0.13
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.