0% found this document useful (0 votes)
3 views

Pie Charts in Python

Uploaded by

medimat27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Pie Charts in Python

Uploaded by

medimat27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

6/12/22, 12:02 AM Pie Charts in Python - Jupyter Notebook

In [1]: 

# Pie chart with plotly express


import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'")
df.loc[df['pop'] < 2.e6, 'country'] = 'Other countries' # Represent only large countries
fig = px.pie(df, values='pop', names='country', title='Population of European continent
fig.show()

Population of European continent

0.168%
0.343%
0.614% Germany
0.701% Turkey
0.767% France
0.777%
12.1%
14.1% 0.79% United Kingdom
0.894% Italy
0.929% Spain
10.4% 0.933%
Poland
1.25%
1.29% Romania
1.4% Netherlands
1.54% Greece
10.4% 1.7%
1.73% Portugal
1.75% Belgium
1.77% Czech Republic
9.92% 1.82%
2.

Serbia
83

1.83%
3.8

Hungary
%

6.9%
%

6.57% Sweden
Austria
Switzerland

localhost:8888/notebooks/Pie Charts in Python.ipynb 1/7


6/12/22, 12:02 AM Pie Charts in Python - Jupyter Notebook

In [2]: 

# Pie chart with repeated labels


import plotly.express as px
# This dataframe has 244 lines, but 4 distinct values for `day`
df = px.data.tips()
fig = px.pie(df, values='tip', names='day')
fig.show()

Sat
Sun
Thur
Fri

33.8% 35.6%

23.5%
7.1%

localhost:8888/notebooks/Pie Charts in Python.ipynb 2/7


6/12/22, 12:02 AM Pie Charts in Python - Jupyter Notebook

In [4]: 

# Setting the color of pie sectors with px.pie


import plotly.express as px
df = px.data.tips()
fig = px.pie(df, values='tip', names='day',
color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()

Sat
Sun
Thur
Fri

33.8% 35.6%

23.5%
7.1%

localhost:8888/notebooks/Pie Charts in Python.ipynb 3/7


6/12/22, 12:02 AM Pie Charts in Python - Jupyter Notebook

In [5]: 

# Using an explicit mapping for discrete colors


import plotly.express as px
df = px.data.tips()
fig = px.pie(df, values='tip', names='day', color='day',
color_discrete_map={'Thur':'lightcyan',
'Fri':'cyan',
'Sat':'royalblue',
'Sun':'darkblue'})
fig.show()

Sat
Sun
Thur
Fri

33.8% 35.6%

23.5%
7.1%

localhost:8888/notebooks/Pie Charts in Python.ipynb 4/7


6/12/22, 12:02 AM Pie Charts in Python - Jupyter Notebook

In [6]: 

# Customizing a pie chart created with px.pie


import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent == 'Americas'")
fig = px.pie(df, values='pop', names='country',
title='Population of American continent',
hover_data=['lifeExp'], labels={'lifeExp':'life expectancy'})
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()

Population of American continent

Venezuela
Chile
Ecuador
Guatemala
Brazil Cuba
21.1% Dominican Republic
United States Bolivia
33.5% Haiti
Honduras
Mexico El Salvador
12.1% Paraguay
Nicaragua
a Costa Rica
bi
m
lo % Puerto Rico
C o .92
Ja ma
0 ic
P a .3 0 9 a
0. na %
ma
Ur 36
48 na

Pu ug 1%
er 0.38 ua y
0. t o 4%
Co 43 Ri
st 9% co

4
N 0. a Ri

Uruguay
i ca 46
% ca
P
a 0. 63 ragu
4. nti

E
l 0 ra 1% a
S .7 4 g u
%

0 a a
.7 l v 2 % y
7 ad
Ho .8

2
ge

% or
Do

nd 33
3 .7 d a

0 Hait %
m

Panama
ur %
in 1.04

as
0.9 liv
ic
1%

Venezuela
Ar

an %

i
4 ia
1 .0 ublic

6
o
Gu
na

Re
3 .1 9 %

1
2.9%

ate %

%
Cu %
P e ru

Jamaica
1 .2
1
Ec u %
.4

ba
Ca

ma
1.53

7
1.81%
Chil e

ado

la
r

Trinidad and Tobago

localhost:8888/notebooks/Pie Charts in Python.ipynb 5/7


6/12/22, 12:02 AM Pie Charts in Python - Jupyter Notebook

In [7]: 

# Basic Pie Chart with go.Pie


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

Oxygen
Hydrogen
Carbon_Dioxide
Nitrogen

29.2%

52.6%

12.3%

5.85%

localhost:8888/notebooks/Pie Charts in Python.ipynb 6/7


6/12/22, 12:02 AM Pie Charts in Python - Jupyter Notebook

In [8]: 

# Styled Pie Chart

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

Oxygen
Hydrogen
Carbon_Dioxide
Nitrogen

2500

4500

1053

500

localhost:8888/notebooks/Pie Charts in Python.ipynb 7/7

You might also like