How to create a Pie Chart in Plotly Python

Spread the love

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 = ""
df = pd.read_csv(url)
df['Expenditure'] = df['Expenditure'].str.replace('$', '').str.replace(',', '')
df['Expenditure'] = df['Expenditure'].astype(float)

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 as px
fig = px.pie(df, values='Expenditure', names='Category', title="Expenditure BY Category")

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', 
fig.update_layout(title="Expenditure By Category")

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

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

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

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

Rating: 1 out of 5.

Leave a Reply