Pie & Donut Chart in Plotly
Last Updated: 08 Nov 2025
- Pie charts show proportions of a whole — perfect for market share, budget breakdown, survey results, or category distribution.
- Donut = Pie with a hole in the center — looks modern & allows center text.
Hinglish Tip: Pie chart = "total ka hissa" — jaise pizza ke slices!. Donut = Pizza with hole — center mein total ya label daal sakte ho.
Basic Pie Chart
import plotly.express as px
# Sample data
labels = ['Rent', 'Food', 'Transport', 'Entertainment', 'Savings']
values = [30000, 15000, 8000, 5000, 12000]
fig = px.pie(names=labels, values=values, title="Monthly Budget")
fig.show()
Key Parameters in px.pie()
| Parameter | Values / Examples | Purpose |
|---|---|---|
| names | List or column | Category labels |
| values | List or column | Size of each slice |
| color_discrete_sequence | px.colors.qualitative.Set3 | Custom colors |
| hole | 0.4 (0 = pie, >0 = donut) | Make it a donut chart |
| title | String | Chart title |
| labels | 'names': 'Category' | Rename legend |
| hover_data | ['extra'] | Extra info on hover |
| textinfo | 'label+percent', 'value' | What to show on slice |
| textposition | 'inside', 'outside' | Label placement |
Full Example
import plotly.express as px
import pandas as pd
# Sample data
df = pd.DataFrame({
'Category': ['Marketing', 'Development', 'Sales', 'HR', 'Support'],
'Budget': [25000, 40000, 18000, 12000, 10000]
})
fig = px.pie(
df,
names='Category',
values='Budget',
hole=0.5, # Donut!
title='Annual Department Budget',
color_discrete_sequence=px.colors.qualitative.Pastel,
labels={'Budget': 'Amount (₹)'}
)
# Customize labels on slices
fig.update_traces(
textinfo='percent+label',
textposition='inside',
hovertemplate='<b>%{label}</b><br>₹%{value:,.0f}<br>%{percent}'
)
# Add center text
fig.update_layout(
annotations=[
dict(
text='₹105K<br><sub>Total Budget</sub>',
x=0.5, y=0.5,
font_size=16,
showarrow=False
)
],
legend_title="Department",
font=dict(size=12)
)
fig.show()
Hinglish Tip: hole=0.5 + annotations → center mein total dikhao — report mein pro lagta hai!
Simple Pie with Pull-Out Slice
fig = px.pie(
df, names='Category', values='Budget',
title='Highlight Largest Slice'
)
# Pull out the largest slice
fig.update_traces(pull=[0.1, 0, 0, 0, 0]) # 10% pull on first slice
fig.show()
Donut with Custom Colors & Outside Labels
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#c2c2f0']
fig = px.pie(
df, names='Category', values='Budget',
hole=0.4,
color_discrete_sequence=colors,
title='Donut with Outside Labels'
)
fig.update_traces(textposition='outside', textinfo='label+percent')
fig.update_layout(showlegend=False)
fig.show()
Hinglish Tip: textposition='outside' → labels overlap nahi honge — clean for small screens!
Common Mistakes to Avoid
| Mistake | Fix |
|---|---|
| Too many slices → messy | Limit to 5–7 categories. Group small ones into "Others" |
| No % on slice | Use textinfo='percent+label' |
| Labels overlap | Use textposition='outside' or increase hole |
| No total in donut | Add annotations in center |
| Can't export image | Install kaleido: pip install -U kaleido |