Project 32: Budget Pie Party 🥧¶

Difficulty: 🟡 Intermediate

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/budget_pie_party/.

← Back to index

Description¶

Turn a monthly budget into a colorful pie chart and a plain-English spend story.

Libraries & Modules¶

  • matplotlib.pyplot
  • Built-in dicts

Python Concepts You'll Practice¶

Percentages, pie charts, narrative summaries from data

🎛️ Parameter Variations¶

  • BUDGET amounts
  • TITLE for the chart
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "budget_pie_party"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\budget_pie_party

Step 1 — Add up the month¶

In [3]:
# 🎛️ TWEAK THESE
BUDGET = {
    "Rent": 1200,
    "Food": 420,
    "Transit": 120,
    "Fun": 200,
    "Savings": 350,
    "Misc": 90,
}
TOTAL = sum(BUDGET.values())
print(f"Monthly total: ${TOTAL}")
for k, v in BUDGET.items():
    print(f"  {k:8s} ${v:6.0f}  ({v / TOTAL:.0%})")
Monthly total: $2380
  Rent     $  1200  (50%)
  Food     $   420  (18%)
  Transit  $   120  (5%)
  Fun      $   200  (8%)
  Savings  $   350  (15%)
  Misc     $    90  (4%)

Step 2 — Pie + story export¶

In [4]:
import matplotlib.pyplot as plt

labels = list(BUDGET.keys())
sizes = list(BUDGET.values())
fig, ax = plt.subplots(figsize=(7, 7))
ax.pie(sizes, labels=labels, autopct="%1.0f%%", startangle=90,
       colors=["#111111", "#8b1e1e", "#444444", "#777777", "#bbbbbb", "#e5e5e5"])
ax.set_title("Budget Pie Party — CodeItAll")
fig.tight_layout()
chart = OUTPUT_DIR / "budget_pie.png"
fig.savefig(chart, dpi=150)
plt.show()

top = max(BUDGET, key=BUDGET.get)
story = (
    f"This month you planned ${TOTAL}. "
    f"Your biggest slice is {top} at ${BUDGET[top]} "
    f"({BUDGET[top] / TOTAL:.0%} of the pie)."
)
(OUTPUT_DIR / "budget_story.txt").write_text(story, encoding="utf-8")
print(story)
No description has been provided for this image
This month you planned $2380. Your biggest slice is Rent at $1200 (50% of the pie).

✅ Project complete¶

Check your output folder: outputs/budget_pie_party/

Next: Project 33: Calendar Heatmap Diary · Index