- Notifications
You must be signed in to change notification settings - Fork1
pie chart notebook#4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -11,6 +11,16 @@ jupyter: | ||
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.7 | ||
plotly: | ||
description: How to make Pie Charts. | ||
display_as: basic | ||
@@ -24,210 +34,138 @@ jupyter: | ||
permalink: python/pie-charts/ | ||
thumbnail: thumbnail/pie-chart.jpg | ||
title: Pie Charts in Python | plotly | ||
v4upgrade: true | ||
--- | ||
### Basic Pie Chart ### | ||
A pie chart ``go.Pie`` object is a circular statistical chart, which is divided into sectors to illustrate numerical proportion. Data visualized by the sectors of the pie is set in `values`. The sector labels are set in `labels`. The sector colors are set in `marker.colors`. | ||
```python | ||
import plotly.graph_objects as go | ||
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'] | ||
values = [4500, 2500, 1053, 500] | ||
fig = go.Figure(data=[go.Pie(labels=labels, values=values)]) | ||
fig.show() | ||
``` | ||
### Styled Pie Chart | ||
Colors can be given as RGB triplets or hexadecimal strings, or with [CSS color names](https://www.w3schools.com/cssref/css_colors.asp) as below. | ||
```python | ||
import plotly.graph_objects as go | ||
colors = ['gold', 'mediumturquoise', 'darkorange', 'lightgreen'] | ||
fig = go.Figure(data=[go.Pie(labels=['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'], | ||
values=[4500,2500,1053,500])]) | ||
fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=20, | ||
marker=dict(colors=colors, line=dict(color='#000000', width=2))) | ||
fig.show() | ||
``` | ||
### Donut Chart | ||
emmanuelle marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
```python | ||
import plotly.graph_objects as go | ||
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'] | ||
values = [4500,2500,1053,500] | ||
# Use `hole` to create a donut-like pie chart | ||
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)]) | ||
fig.show() | ||
``` | ||
### PieCharts in subplots | ||
```python | ||
import plotly.graph_objects as go | ||
from plotly.subplots import make_subplots | ||
labels = ["US", "China", "European Union", "Russian Federation", "Brazil", "India", | ||
"Rest of World"] | ||
# Create subplots: use 'domain' type for Pie subplot | ||
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]]) | ||
fig.add_trace(go.Pie(labels=labels, values=[16, 15, 12, 6, 5, 4, 42], name="GHG Emissions"), | ||
1, 1) | ||
fig.add_trace(go.Pie(labels=labels, values=[27, 11, 25, 8, 1, 3, 25], name="CO2 Emissions"), | ||
1, 2) | ||
# Use `hole` to create a donut-like pie chart | ||
fig.update_traces(hole=.4, hoverinfo="label+percent+name") | ||
fig.update_layout( | ||
title_text="Global Emissions 1990-2011", | ||
# Add annotations in the center of the donut pies. | ||
annotations=[dict(text='GHG', x=0.18, y=0.5, font_size=20, showarrow=False), | ||
dict(text='CO2', x=0.82, y=0.5, font_size=20, showarrow=False)]) | ||
fig.show() | ||
``` | ||
```python | ||
import plotly.graph_objects as go | ||
from plotly.subplots import make_subplots | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Neither of these examples uses the very-cool There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Which type of layout were you thinking of? One small pie chart inside another one with a hole? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. A simple example would be two side by side pies that show the same 4 categories but in different years, say. If you put them in the same scalegroup then the radius is auto-scaled appropriately. Here's an example (generated with myhttps://react-pivottable.js.org/ UI :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. ok, I added such an example. | ||
labels = ['1st', '2nd', '3rd', '4th', '5th'] | ||
# Define color sets of paintings | ||
night_colors = ['rgb(56, 75, 126)', 'rgb(18, 36, 37)', 'rgb(34, 53, 101)', | ||
'rgb(36, 55, 57)', 'rgb(6, 4, 4)'] | ||
sunflowers_colors = ['rgb(177, 127, 38)', 'rgb(205, 152, 36)', 'rgb(99, 79, 37)', | ||
'rgb(129, 180, 179)', 'rgb(124, 103, 37)'] | ||
irises_colors = ['rgb(33, 75, 99)', 'rgb(79, 129, 102)', 'rgb(151, 179, 100)', | ||
'rgb(175, 49, 35)', 'rgb(36, 73, 147)'] | ||
cafe_colors = ['rgb(146, 123, 21)', 'rgb(177, 180, 34)', 'rgb(206, 206, 40)', | ||
'rgb(175, 51, 21)', 'rgb(35, 36, 21)'] | ||
# Create subplots, using 'domain' type for pie charts | ||
specs = [[{'type':'domain'}, {'type':'domain'}], [{'type':'domain'}, {'type':'domain'}]] | ||
fig = make_subplots(rows=2, cols=2, specs=specs) | ||
# Define pie charts | ||
fig.add_trace(go.Pie(labels=labels, values=[38, 27, 18, 10, 7], name='Starry Night', | ||
marker_colors=night_colors), 1, 1) | ||
fig.add_trace(go.Pie(labels=labels, values=[28, 26, 21, 15, 10], name='Sunflowers', | ||
marker_colors=sunflowers_colors), 1, 2) | ||
fig.add_trace(go.Pie(labels=labels, values=[38, 19, 16, 14, 13], name='Irises', | ||
marker_colors=irises_colors), 2, 1) | ||
fig.add_trace(go.Pie(labels=labels, values=[31, 24, 19, 18, 8], name='The Night Café', | ||
marker_colors=cafe_colors), 2, 2) | ||
# Tune layout and hover info | ||
fig.update_traces(hoverinfo='label+percent+name', textinfo='none') | ||
fig.update(layout_title_text='Van Gogh: 5 Most Prominent Colors Shown Proportionally', | ||
layout_showlegend=False) | ||
fig = go.Figure(fig) | ||
fig.show() | ||
``` | ||
#### Plot chart with area proportional to total count | ||
Plots inthesame `scalegroup` are represented with an area proportional to their total size. | ||
```python | ||
import plotly.graph_objects as go | ||
from plotly.subplots import make_subplots | ||
labels = ["Asia", "Europe", "Africa", "Americas", "Oceania"] | ||
fig = make_subplots(1, 2, specs=[[{'type':'domain'}, {'type':'domain'}]], | ||
subplot_titles=['1980', '2007']) | ||
fig.add_trace(go.Pie(labels=labels, values=[4, 7, 1, 7, 0.5], scalegroup='one', | ||
name="World GDP 1980"), 1, 1) | ||
fig.add_trace(go.Pie(labels=labels, values=[21, 15, 3, 19, 1], scalegroup='one', | ||
name="World GDP 2007"), 1, 2) | ||
fig.update_layout(title_text='World GDP') | ||
fig.show() | ||
``` | ||
### Dash Example | ||
@@ -247,21 +185,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-pieplot/code", width="100 | ||
#### Reference | ||
See https://plot.ly/python/reference/#pie for more information and chart attribute options! | ||