Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
emmanuelle merged 5 commits intomasterfrompie
Jun 28, 2019
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
306 changes: 113 additions & 193 deletionsnotebooks/pie-charts.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -24,210 +34,138 @@ jupyter:
permalink: python/pie-charts/
thumbnail: thumbnail/pie-chart.jpg
title: Pie Charts in Python | plotly
v4upgrade: true
---

#### New to Plotly?
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
### 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]

#### Version Check
Note: Pie Charts are available in version <b>1.9.12+</b><br>
Run `pip install plotly --upgrade` to update your Plotly version
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
plotly.__version__
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()
```

### Basic Pie Chart ###
### Donut Chart


```python
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.graph_objects as go

labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
values = [4500,2500,1053,500]

trace = go.Pie(labels=labels, values=values)

py.iplot([trace], filename='basic_pie_chart')
# Use `hole` to create a donut-like pie chart
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])
fig.show()
```

###StyledPieChart
### PieCharts in subplots

```python
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')
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()
```

### Donut Chart
This example uses a [plotly grid attribute](https://plot.ly/python/reference/#layout-grid) for the suplots. Reference the row and column destination using the [domain](https://plot.ly/python/reference/#pie-domain) attribute.

```python
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')
import plotly.graph_objects as go
from plotly.subplots import make_subplots
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Neither of these examples uses the very-coolscalegroup attr that allows for automatic relative scaling of pie charts, and I think it would be great to add one!

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 :)

image

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The 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()
```

### Pie Chart Subplots ###

#### Plot chart with area proportional to total count

In order to create pie chart subplots, you need to usethe[domain](https://plot.ly/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.
Plots inthesame `scalegroup` are represented with an area proportional to their total size.

```python
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')
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
Expand All@@ -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!

```python
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/')
```

[8]ページ先頭

©2009-2025 Movatter.jp