
In this post, You will learn how to create a pie chart in plotly python
1 . Create a Pie Chart with Plotly Express –
Read a dataset to work with
import pandas as pd
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/QueenCity.csv"
df = pd.read_csv(url)
df['Expenditure'] = df['Expenditure'].str.replace('$', '').str.replace(',', '')
df['Expenditure'] = df['Expenditure'].astype(float)
df.head()

A pie chart is a circular statistical chart, which is divided into sectors to illustrate numerical proportion.
To create a pie chart in plotly express, you have to use px.pie() method.
import plotly.express as px
fig = px.pie(df, values='Expenditure', names='Category', title="Expenditure BY Category")
fig.show()

To change the color of the pie chart, you can use color_discrete_sequence parameter. There are many colors, choose the one that you like most.
fig = px.pie(df, values='Expenditure', names='Category',
color_discrete_sequence=px.colors.sequential.RdBu)
fig.update_layout(title="Expenditure By Category")
fig.show()

2 . Create a Pie chart with Plotly Graph Objects –
To create a pie chart with Plotly Graph Objects, you have to use go.Pie() method.
import plotly.graph_objects as go
labels = ['Fri', 'Sat', 'Sun', 'Thur']
values = [325.88, 1778.40, 1627.16, 1096.33]
fig = go.Figure()
fig.add_trace(go.Pie(labels=labels, values=values))
fig.update_layout(title='Total Bill By Day')
fig.show()

Donut Chart –
Use the hole parameter to create a Donut chart
labels = ['Fri', 'Sat', 'Sun', 'Thur']
values = [325.88, 1778.40, 1627.16, 1096.33]
fig = go.Figure()
fig.add_trace(go.Pie(labels=labels, values=values, hole=0.3))
fig.update_layout(title='Total Bill By Day')
fig.show()

Pull sectors out of center –
For a “pulled-out” or “exploded” layout of the pie chart, use the pull argument.
fig = go.Figure()
fig.add_trace(go.Pie(labels=labels, values=values, pull=[0.2, 0, 0, 0]))
fig.update_layout(title='Total Bill By Day')
fig.show()

Related Posts –
1 . How to install plotly python with pip
2 . How to create a Line Chart with Plotly Python
3 . How to create Scatter plot in Plotly Python
4 . How to create a Bar Chart in Plotly Python
5 . How to create Horizontal Bar Chart in Plotly Python
6 . How to create a Histogram in plotly python
7 . How to Create a Box Plot in Plotly Python
8 . How to create a Dot Plot in Plotly Python
9 . How to Create Heatmap with Plotly Python
10 . How to Create a Violin Plot in Plotly Python
11 . How to Create Subplots in Plotly Python