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

Commiteaca0e9

Browse files
authored
Merge pull request#4 from plotly/pie
pie chart notebook
2 parentsb718c8b +646c4c3 commiteaca0e9

File tree

1 file changed

+113
-193
lines changed

1 file changed

+113
-193
lines changed

‎notebooks/pie-charts.md

Lines changed: 113 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ jupyter:
1111
display_name:Python 3
1212
language:python
1313
name:python3
14+
language_info:
15+
codemirror_mode:
16+
name:ipython
17+
version:3
18+
file_extension:.py
19+
mimetype:text/x-python
20+
name:python
21+
nbconvert_exporter:python
22+
pygments_lexer:ipython3
23+
version:3.6.7
1424
plotly:
1525
description:How to make Pie Charts.
1626
display_as:basic
@@ -24,210 +34,138 @@ jupyter:
2434
permalink:python/pie-charts/
2535
thumbnail:thumbnail/pie-chart.jpg
2636
title:Pie Charts in Python | plotly
37+
v4upgrade:true
2738
---
2839

29-
####New to Plotly?
30-
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/).
31-
<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).
32-
<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!
40+
###Basic Pie Chart ###
41+
42+
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`.
3343

44+
```python
45+
import plotly.graph_objectsas go
3446

47+
labels= ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
48+
values= [4500,2500,1053,500]
3549

36-
####Version Check
37-
Note: Pie Charts are available in version <b>1.9.12+</b><br>
38-
Run`pip install plotly --upgrade` to update your Plotly version
50+
fig= go.Figure(data=[go.Pie(labels=labels,values=values)])
51+
fig.show()
52+
```
3953

54+
###Styled Pie Chart
4055

56+
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.
4157

4258
```python
43-
import plotly
44-
plotly.__version__
59+
import plotly.graph_objectsas go
60+
colors= ['gold','mediumturquoise','darkorange','lightgreen']
61+
62+
fig= go.Figure(data=[go.Pie(labels=['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'],
63+
values=[4500,2500,1053,500])])
64+
fig.update_traces(hoverinfo='label+percent',textinfo='value',textfont_size=20,
65+
marker=dict(colors=colors,line=dict(color='#000000',width=2)))
66+
fig.show()
4567
```
4668

47-
###Basic Pie Chart ###
69+
###Donut Chart
70+
4871

4972
```python
50-
import plotly.plotlyas py
51-
import plotly.graph_objsas go
73+
import plotly.graph_objectsas go
5274

5375
labels= ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
54-
values= [4500,2500,1053,500]
76+
values= [4500,2500,1053,500]
5577

56-
trace= go.Pie(labels=labels,values=values)
57-
58-
py.iplot([trace],filename='basic_pie_chart')
78+
# Use `hole` to create a donut-like pie chart
79+
fig= go.Figure(data=[go.Pie(labels=labels,values=values,hole=.3)])
80+
fig.show()
5981
```
6082

61-
###StyledPieChart
83+
###PieCharts in subplots
6284

6385
```python
64-
import plotly.plotlyas py
65-
import plotly.graph_objsas go
66-
67-
labels= ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
68-
values= [4500,2500,1053,500]
69-
colors= ['#FEBFB3','#E1396C','#96D38C','#D0F9B1']
70-
71-
trace= go.Pie(labels=labels,values=values,
72-
hoverinfo='label+percent',textinfo='value',
73-
textfont=dict(size=20),
74-
marker=dict(colors=colors,
75-
line=dict(color='#000000',width=2)))
76-
77-
py.iplot([trace],filename='styled_pie_chart')
86+
import plotly.graph_objectsas go
87+
from plotly.subplotsimport make_subplots
88+
89+
labels= ["US","China","European Union","Russian Federation","Brazil","India",
90+
"Rest of World"]
91+
92+
# Create subplots: use 'domain' type for Pie subplot
93+
fig= make_subplots(rows=1,cols=2,specs=[[{'type':'domain'}, {'type':'domain'}]])
94+
fig.add_trace(go.Pie(labels=labels,values=[16,15,12,6,5,4,42],name="GHG Emissions"),
95+
1,1)
96+
fig.add_trace(go.Pie(labels=labels,values=[27,11,25,8,1,3,25],name="CO2 Emissions"),
97+
1,2)
98+
99+
# Use `hole` to create a donut-like pie chart
100+
fig.update_traces(hole=.4,hoverinfo="label+percent+name")
101+
102+
fig.update_layout(
103+
title_text="Global Emissions 1990-2011",
104+
# Add annotations in the center of the donut pies.
105+
annotations=[dict(text='GHG',x=0.18,y=0.5,font_size=20,showarrow=False),
106+
dict(text='CO2',x=0.82,y=0.5,font_size=20,showarrow=False)])
107+
fig.show()
78108
```
79109

80-
###Donut Chart
81-
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.
82110

83111
```python
84-
import plotly.plotlyas py
85-
import plotly.graph_objsas go
86-
87-
fig= {
88-
"data": [
89-
{
90-
"values": [16,15,12,6,5,4,42],
91-
"labels": [
92-
"US",
93-
"China",
94-
"European Union",
95-
"Russian Federation",
96-
"Brazil",
97-
"India",
98-
"Rest of World"
99-
],
100-
"domain": {"column":0},
101-
"name":"GHG Emissions",
102-
"hoverinfo":"label+percent+name",
103-
"hole":.4,
104-
"type":"pie"
105-
},
106-
{
107-
"values": [27,11,25,8,1,3,25],
108-
"labels": [
109-
"US",
110-
"China",
111-
"European Union",
112-
"Russian Federation",
113-
"Brazil",
114-
"India",
115-
"Rest of World"
116-
],
117-
"text":["CO2"],
118-
"textposition":"inside",
119-
"domain": {"column":1},
120-
"name":"CO2 Emissions",
121-
"hoverinfo":"label+percent+name",
122-
"hole":.4,
123-
"type":"pie"
124-
}],
125-
"layout": {
126-
"title":"Global Emissions 1990-2011",
127-
"grid": {"rows":1,"columns":2},
128-
"annotations": [
129-
{
130-
"font": {
131-
"size":20
132-
},
133-
"showarrow":False,
134-
"text":"GHG",
135-
"x":0.20,
136-
"y":0.5
137-
},
138-
{
139-
"font": {
140-
"size":20
141-
},
142-
"showarrow":False,
143-
"text":"CO2",
144-
"x":0.8,
145-
"y":0.5
146-
}
147-
]
148-
}
149-
}
150-
py.iplot(fig,filename='donut')
112+
import plotly.graph_objectsas go
113+
from plotly.subplotsimport make_subplots
114+
115+
labels= ['1st','2nd','3rd','4th','5th']
116+
117+
# Define color sets of paintings
118+
night_colors= ['rgb(56, 75, 126)','rgb(18, 36, 37)','rgb(34, 53, 101)',
119+
'rgb(36, 55, 57)','rgb(6, 4, 4)']
120+
sunflowers_colors= ['rgb(177, 127, 38)','rgb(205, 152, 36)','rgb(99, 79, 37)',
121+
'rgb(129, 180, 179)','rgb(124, 103, 37)']
122+
irises_colors= ['rgb(33, 75, 99)','rgb(79, 129, 102)','rgb(151, 179, 100)',
123+
'rgb(175, 49, 35)','rgb(36, 73, 147)']
124+
cafe_colors= ['rgb(146, 123, 21)','rgb(177, 180, 34)','rgb(206, 206, 40)',
125+
'rgb(175, 51, 21)','rgb(35, 36, 21)']
126+
127+
# Create subplots, using 'domain' type for pie charts
128+
specs= [[{'type':'domain'}, {'type':'domain'}], [{'type':'domain'}, {'type':'domain'}]]
129+
fig= make_subplots(rows=2,cols=2,specs=specs)
130+
131+
# Define pie charts
132+
fig.add_trace(go.Pie(labels=labels,values=[38,27,18,10,7],name='Starry Night',
133+
marker_colors=night_colors),1,1)
134+
fig.add_trace(go.Pie(labels=labels,values=[28,26,21,15,10],name='Sunflowers',
135+
marker_colors=sunflowers_colors),1,2)
136+
fig.add_trace(go.Pie(labels=labels,values=[38,19,16,14,13],name='Irises',
137+
marker_colors=irises_colors),2,1)
138+
fig.add_trace(go.Pie(labels=labels,values=[31,24,19,18,8],name='The Night Café',
139+
marker_colors=cafe_colors),2,2)
140+
141+
# Tune layout and hover info
142+
fig.update_traces(hoverinfo='label+percent+name',textinfo='none')
143+
fig.update(layout_title_text='Van Gogh: 5 Most Prominent Colors Shown Proportionally',
144+
layout_showlegend=False)
145+
146+
fig= go.Figure(fig)
147+
fig.show()
151148
```
152149

153-
###Pie Chart Subplots ###
154-
150+
####Plot chart with area proportional to total count
155151

156-
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.
152+
Plots inthesame`scalegroup` are represented with an area proportional to their total size.
157153

158154
```python
159-
import plotly.plotlyas py
160-
import plotly.graph_objsas go
161-
162-
fig= {
163-
'data': [
164-
{
165-
'labels': ['1st','2nd','3rd','4th','5th'],
166-
'values': [38,27,18,10,7],
167-
'type':'pie',
168-
'name':'Starry Night',
169-
'marker': {'colors': ['rgb(56, 75, 126)',
170-
'rgb(18, 36, 37)',
171-
'rgb(34, 53, 101)',
172-
'rgb(36, 55, 57)',
173-
'rgb(6, 4, 4)']},
174-
'domain': {'x': [0,.48],
175-
'y': [0,.49]},
176-
'hoverinfo':'label+percent+name',
177-
'textinfo':'none'
178-
},
179-
{
180-
'labels': ['1st','2nd','3rd','4th','5th'],
181-
'values': [28,26,21,15,10],
182-
'marker': {'colors': ['rgb(177, 127, 38)',
183-
'rgb(205, 152, 36)',
184-
'rgb(99, 79, 37)',
185-
'rgb(129, 180, 179)',
186-
'rgb(124, 103, 37)']},
187-
'type':'pie',
188-
'name':'Sunflowers',
189-
'domain': {'x': [.52,1],
190-
'y': [0,.49]},
191-
'hoverinfo':'label+percent+name',
192-
'textinfo':'none'
193-
194-
},
195-
{
196-
'labels': ['1st','2nd','3rd','4th','5th'],
197-
'values': [38,19,16,14,13],
198-
'marker': {'colors': ['rgb(33, 75, 99)',
199-
'rgb(79, 129, 102)',
200-
'rgb(151, 179, 100)',
201-
'rgb(175, 49, 35)',
202-
'rgb(36, 73, 147)']},
203-
'type':'pie',
204-
'name':'Irises',
205-
'domain': {'x': [0,.48],
206-
'y': [.51,1]},
207-
'hoverinfo':'label+percent+name',
208-
'textinfo':'none'
209-
},
210-
{
211-
'labels': ['1st','2nd','3rd','4th','5th'],
212-
'values': [31,24,19,18,8],
213-
'marker': {'colors': ['rgb(146, 123, 21)',
214-
'rgb(177, 180, 34)',
215-
'rgb(206, 206, 40)',
216-
'rgb(175, 51, 21)',
217-
'rgb(35, 36, 21)']},
218-
'type':'pie',
219-
'name':'The Night Café',
220-
'domain': {'x': [.52,1],
221-
'y': [.51,1]},
222-
'hoverinfo':'label+percent+name',
223-
'textinfo':'none'
224-
}
225-
],
226-
'layout': {'title':'Van Gogh: 5 Most Prominent Colors Shown Proportionally',
227-
'showlegend':False}
228-
}
229-
230-
py.iplot(fig,filename='pie_chart_subplots')
155+
import plotly.graph_objectsas go
156+
from plotly.subplotsimport make_subplots
157+
158+
labels= ["Asia","Europe","Africa","Americas","Oceania"]
159+
160+
fig= make_subplots(1,2,specs=[[{'type':'domain'}, {'type':'domain'}]],
161+
subplot_titles=['1980','2007'])
162+
fig.add_trace(go.Pie(labels=labels,values=[4,7,1,7,0.5],scalegroup='one',
163+
name="World GDP 1980"),1,1)
164+
fig.add_trace(go.Pie(labels=labels,values=[21,15,3,19,1],scalegroup='one',
165+
name="World GDP 2007"),1,2)
166+
167+
fig.update_layout(title_text='World GDP')
168+
fig.show()
231169
```
232170

233171
###Dash Example
@@ -247,21 +185,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-pieplot/code", width="100
247185

248186
####Reference
249187
Seehttps://plot.ly/python/reference/#pie for more information and chart attribute options!
250-
251-
```python
252-
from IPython.displayimport display,HTML
253-
254-
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" />'))
255-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
256-
257-
! pip install git+https://github.com/plotly/publisher.git--upgrade
258-
import publisher
259-
publisher.publish(
260-
'pie-charts.ipynb','python/pie-charts/','Pie Charts',
261-
'How to make Pie Charts.',
262-
title='Pie Charts in Python | plotly',
263-
has_thumbnail='true',thumbnail='thumbnail/pie-chart.jpg',
264-
language='python',page_type='example_index',
265-
display_as='basic',order=6,
266-
ipynb='~notebook_demo/7/')
267-
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp