๐Ÿš€ Phase 9: Build a Visual Expense Dashboard (Matplotlib)

This guide will show you exactly how to create a professional expense dashboard from your CSV file. The code block is ready to paste into your dashboard.py.

๐Ÿ“ File Structure

๐Ÿ“ your_project_folder/
โ”œโ”€โ”€ receipt_reader.py
โ”œโ”€โ”€ dashboard.py         โ† you paste this here
โ”œโ”€โ”€ receipt.jpg
โ””โ”€โ”€ expenses.csv          โ† created automatically
      

๐ŸŽฏ GOAL

Cleaning  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Food      โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Personal  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Others    โ–ˆโ–ˆโ–ˆโ–ˆ

โœ… What You Need

pip install matplotlib

This command installs the matplotlib library needed to draw the bar chart.

โœ… Step-by-Step Dashboard Script

Create a new Python file:

๐Ÿ“„ dashboard.py

import csv
from collections import defaultdict
import matplotlib.pyplot as plt

# Step 1: Load categorized data from CSV
category_totals = defaultdict(float)

with open('expenses.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        category = row['Category']
        price = float(row['Price'])
        category_totals[category] += price

# Step 2: Prepare data for chart
categories = list(category_totals.keys())
totals = list(category_totals.values())

# Step 3: Create bar chart
plt.figure(figsize=(10, 6))
bars = plt.bar(categories, totals, color='skyblue')
plt.title('Total Expenses by Category')
plt.xlabel('Category')
plt.ylabel('Total (โ‚ฆ)')
plt.xticks(rotation=30)
plt.grid(axis='y')

# Step 4: Add price labels on bars
for bar in bars:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2, yval + 5, f"โ‚ฆ{int(yval)}", ha='center', fontsize=9)

# Show the chart
plt.tight_layout()
plt.show()
        

๐Ÿ“Š What This Does

Step Description
Load CSV Reads your expense data from expenses.csv
Group Data Adds prices for each category using a dictionary
Plot Chart Draws a bar chart using matplotlib

๐Ÿงช How to Run

python dashboard.py

This runs your Python dashboard script and shows the expense bar chart.