How to Create a Pie Chart in Python Using Plotly and Excel
Read the Excel sheet into pandas, pass the value and category columns to plotly.express.pie(), show the labels inside the slices, and save the chart as an HTML file. The result is interactive: hover for exact values and click the legend to hide categories. Edit the sheet and rerun the script to update it.
Install the libraries
The script needs plotly for the chart, and pandas plus openpyxl to read the .xlsx file. Open a command prompt (Windows+R, type cmd, press Enter) and install them:
pip install plotly
pip install pandas openpyxl
Changed since the video: the video also installs plotly_express and xlrd, which you no longer need: Plotly Express ships with plotly, and xlrd 2.0 and later cannot read .xlsx files.
The Excel data
The workbook data.xlsx holds survey results: the answer categories in column A and the number of votes for each in column B. Save it in the same folder as the script, or use the full path in read_excel().

Build the chart
Create a file called pie_chart_python.py. A Plotly Express pie chart needs two things from the sheet: the values that size the slices and the names that label them.
Import the libraries:
import plotly.express as px
import pandas as pd
import plotly
Read the sheet into a DataFrame:
df = pd.read_excel('data.xlsx')
Store the vote counts and the category names in two variables:
values = df['Results']
names = df['Category']
Create the pie chart with a title:
fig = px.pie(df, values=values, names=names, title='Survey Results')
Show the label and the percentage inside each slice:
fig.update_traces(textposition='inside', textinfo='percent+label')
Make the title larger:
fig.update_layout(title_font_size=42)
Save the chart as an HTML file. plotly.offline.plot() writes the file and opens it in your browser; fig.write_html(‘Piechart.html’) writes it without opening a window.
plotly.offline.plot(fig, filename='Piechart.html')
Here is the complete script in one piece:
# Imports
import plotly.express as px
import pandas as pd
import plotly
# Read data from excel file and store it in a variable: df (dataframe)
df = pd.read_excel('data.xlsx')
# Store the columns in a separate variable
values = df['Results']
names = df['Category']
# Create Pie Chart
fig = px.pie(
df,
values=values,
names=names,
title='Survey Results',
)
# Update Traces
fig.update_traces(
textposition='inside',
textinfo='percent+label',
)
# Update Layout
fig.update_layout(
title_font_size=42,
)
# Export Pie Chart to HTML
plotly.offline.plot(fig, filename='Piechart.html')
Run the script
Run the script from the folder where it is saved. In the command prompt, change to that folder, for example:
cd Desktop
cd PieChartTutorial
Then start the script:
python pie_chart_python.py
Piechart.html opens with the interactive chart. Hover over a slice for its exact value, and click a category in the legend to hide or show it.

Common changes
A few parameters cover most requests. hole turns the pie into a doughnut, color_discrete_sequence swaps the colors, and textinfo decides what each slice shows. sort=False keeps the slices in the order of the Excel rows instead of sorting them by size.
fig = px.pie(
df,
values="Results",
names="Category",
title="Survey Results",
hole=0.4,
color_discrete_sequence=px.colors.qualitative.Pastel,
)
fig.update_traces(textinfo="value+percent", sort=False)
Passing the column names as strings, as here, is the same as passing the two variables from the main script.
To start the chart from Excel with a button, see executing a Python script from Excel with VBA. When your categories have sub-categories, a pie gets crowded; my treemap and sunburst tutorial uses the same Excel-to-Plotly pattern for hierarchies, and the Plotly Express quick fixes polish the styling.
For a one-off chart without writing code, the free chart maker draws a doughnut chart from pasted Excel cells and exports it as interactive HTML, PNG or SVG.
