In Python, make a pie chart with matplotlib’s ax.pie(values, labels=labels, autopct="%1.0f%%"), with pandas’ Series.plot.pie(), or with Plotly’s px.pie(). In R, use base pie(values, labels) or ggplot2 with geom_col() and coord_polar(theta = "y"). Each supports percentage labels and donut charts.
Python and R are two of the most popular languages for data analysis, and each has several ways to draw a pie chart: matplotlib, pandas and Plotly in Python, and base R and Hadley Wickham’s ggplot2 in R. All examples use the same data: a $2,800 monthly budget with rent $1,450 (51.8%), food $600 (21.4%), other $400 (14.3%) and transport $350 (12.5%).
Python: matplotlib
import matplotlib.pyplot as plt
labels = ["Rent", "Food", "Transport", "Other"]
values = [1450, 600, 350, 400]
fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(
values,
labels=labels,
autopct="%1.0f%%", # percentage on each slice
startangle=90, # first slice at 12 o'clock
counterclock=False, # go clockwise
wedgeprops={"edgecolor": "white"},
)
ax.set_title("Monthly budget")
fig.savefig("pie.png", dpi=200, bbox_inches="tight")
plt.show()- Donut chart: add
width=0.4towedgeprops. - Exploded slice: pass
explode=[0.08, 0, 0, 0]. - One decimal place: use
autopct="%1.1f%%". - Transparent PNG: add
transparent=Truetosavefig.
Python: pandas
import pandas as pd
df = pd.DataFrame({"category": ["Rent", "Food", "Transport", "Other"],
"value": [1450, 600, 350, 400]})
ax = df.set_index("category")["value"].plot.pie(autopct="%1.0f%%", ylabel="")
ax.figure.savefig("pie.png", dpi=200, bbox_inches="tight")Python: Plotly (interactive)
import plotly.express as px
fig = px.pie(df, names="category", values="value", hole=0.4) # hole=0 for a pie
fig.update_traces(textinfo="label+percent")
fig.write_html("pie.html")R: base graphics
labels <- c("Rent", "Food", "Transport", "Other")
values <- c(1450, 600, 350, 400)
pct <- round(values / sum(values) * 100)
pie(values,
labels = paste0(labels, " ", pct, "%"),
col = hcl.colors(length(values), "Set 2"),
clockwise = TRUE,
main = "Monthly budget")R: ggplot2
library(ggplot2)
df <- data.frame(category = c("Rent", "Food", "Transport", "Other"),
value = c(1450, 600, 350, 400))
ggplot(df, aes(x = "", y = value, fill = category)) +
geom_col(width = 1, color = "white") +
coord_polar(theta = "y") +
geom_text(aes(label = scales::percent(value / sum(value), accuracy = 1)),
position = position_stack(vjust = 0.5)) +
theme_void() +
labs(title = "Monthly budget", fill = NULL)
ggsave("pie.png", width = 6, height = 6, dpi = 200)ggplot2 has no pie geom: a pie is a single stacked bar wrapped into a circle with coord_polar(). For a donut, use aes(x = 2, …) and add xlim(c(0.5, 2.5)).
Common problems
- Percentages add up to 99% or 101%: each label is rounded on its own. Round with the largest remainder method, or show one decimal place.
- Overlapping labels on small slices: group small categories into “Other”, or move labels into a legend.
- Oval pie: use a square figure size (matplotlib already keeps the pie round).
Key takeaways
- matplotlib: ax.pie() with autopct for percentages and wedgeprops width for donuts.
- pandas: Series.plot.pie() is the shortest route from a DataFrame.
- Plotly: px.pie() gives an interactive chart; hole= makes a donut.
- Base R: pie(); ggplot2: geom_col() plus coord_polar(theta = "y").
- Round with the largest remainder method if the labels must add up to exactly 100%.
Frequently asked questions
How do I show percentages on a matplotlib pie chart?
Pass autopct="%1.0f%%" to ax.pie() for whole percentages, or "%1.1f%%" for one decimal place.
How do I make a donut chart in matplotlib?
Add wedgeprops={"width": 0.4} to ax.pie(). The width is the ring thickness as a fraction of the radius.
Does ggplot2 have a pie chart function?
No. Draw a single stacked bar with geom_col() and wrap it into a circle with coord_polar(theta = "y").