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
.
๐ your_project_folder/ โโโ receipt_reader.py โโโ dashboard.py โ you paste this here โโโ receipt.jpg โโโ expenses.csv โ created automatically
Cleaning โโโโโโโโโโโโโโโโโ Food โโโโโโโโโ Personal โโโโโโโโ Others โโโโ
pip install matplotlib
This command installs the matplotlib library needed to draw the bar chart.
Create a new Python file:
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()
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 |
receipt_reader.py
has already been runexpenses.csv
is in the same folderpython dashboard.py
This runs your Python dashboard script and shows the expense bar chart.