πŸ”„ Quick Recap (Day 21)

  • You explored tabular data with Pandas: creating DataFrames, reading/writing CSVs, filtering, sorting, and summarizing.

🎯 What You’ll Learn Today

  1. What Matplotlib is and when to use it.

  2. How to draw line, bar, and scatter charts.

  3. How to label axes, add titles, and save figures to files.

  4. (Bonus) Plot directly from a Pandas DataFrame.

πŸ“– Why Visualization?

Charts turn raw numbers into stories you can see. A quick plot can reveal patternsβ€”trends, spikes, and outliersβ€”that are hard to spot in a table. Today you’ll learn the basics so you can communicate insights clearly.

πŸ“– Getting Started with Matplotlib

  1. Install Matplotlib (in your virtual environment from Day 16):

    pip install matplotlib
  2. Import and make your first plot:

    import matplotlib.pyplot as plt
    
    days = [1, 2, 3, 4, 5]
    sales = [100, 120, 90, 150, 130]
    
    plt.figure()
    plt.plot(days, sales)
    plt.title("Sales Over Days")
    plt.xlabel("Day")
    plt.ylabel("Sales")
    plt.savefig("chart_line.png")  # saves the image
    print("Saved: chart_line.png")
  • plt.figure() starts a new, clean figure.

  • plt.plot(x, y) draws a line chart.

  • plt.title/plt.xlabel/plt.ylabel add context.

  • plt.savefig() writes the image to disk so you can reuse it.

❝

Tip: If a window pops up instead of saving, that’s fine too. Saving to a file keeps your charts for reports and sharing.

πŸ“– Bar & Scatter Charts

Bar chart (compare categories):

products = ["A", "B", "C"]
sales = [300, 450, 250]

plt.figure()
plt.bar(products, sales)
plt.title("Sales by Product")
plt.xlabel("Product")
plt.ylabel("Units Sold")
plt.savefig("chart_bar.png")
print("Saved: chart_bar.png")

Scatter plot (relationships between two variables):

ad_spend = [10, 20, 30, 40, 50]   # in $
new_users = [15, 28, 40, 52, 65]

plt.figure()
plt.scatter(ad_spend, new_users)
plt.title("Ad Spend vs New Users")
plt.xlabel("Ad Spend ($)")
plt.ylabel("New Users")
plt.savefig("chart_scatter.png")
print("Saved: chart_scatter.png")

πŸ“– Plotting from a DataFrame (Bonus)

If you already have a DataFrame (from Day 21), you can plot directly:

import pandas as pd
import matplotlib.pyplot as plt

sales_df = pd.DataFrame({
    "month": ["Jan","Feb","Mar","Apr"],
    "revenue": [100, 150, 130, 170]
})

plt.figure()
plt.plot(sales_df["month"], sales_df["revenue"])  # line plot from DataFrame columns
plt.title("Monthly Revenue")
plt.xlabel("Month")
plt.ylabel("Revenue")
plt.savefig("chart_monthly_revenue.png")
print("Saved: chart_monthly_revenue.png")

πŸ§™β€β™‚οΈ Take the Wand and Try Yourself

Task: Create a small visualization report with three charts.

  1. Create visualization_basics.py.

  2. Build the following datasets:

    • Days [1,2,3,4,5,6,7] and website visits [120, 130, 125, 160, 180, 170, 190].

    • Products ["Alpha","Beta","Gamma","Delta"] and units [50, 80, 65, 90].

    • Study hours [1,2,3,4,5,6] and quiz scores [60, 62, 65, 70, 78, 85].

  3. Create three charts and save them:

    • Line chart of visits over days β†’ visits_line.png.

    • Bar chart of units by product β†’ units_bar.png.

    • Scatter plot of study hours vs quiz scores β†’ study_scatter.png.

  4. Ensure each chart has a title, x-label, and y-label. Print confirmation lines after each savefig.

Solution Example (visualization_basics.py):

import matplotlib.pyplot as plt

# 1) Line chart
days = [1,2,3,4,5,6,7]
visits = [120,130,125,160,180,170,190]
plt.figure()
plt.plot(days, visits)
plt.title("Website Visits Over a Week")
plt.xlabel("Day")
plt.ylabel("Visits")
plt.savefig("visits_line.png")
print("Saved: visits_line.png")

# 2) Bar chart
products = ["Alpha","Beta","Gamma","Delta"]
units = [50,80,65,90]
plt.figure()
plt.bar(products, units)
plt.title("Units Sold by Product")
plt.xlabel("Product")
plt.ylabel("Units")
plt.savefig("units_bar.png")
print("Saved: units_bar.png")

# 3) Scatter plot
hours = [1,2,3,4,5,6]
scores = [60,62,65,70,78,85]
plt.figure()
plt.scatter(hours, scores)
plt.title("Study Hours vs Quiz Scores")
plt.xlabel("Hours")
plt.ylabel("Score")
plt.savefig("study_scatter.png")
print("Saved: study_scatter.png")

Expected output (terminal prints):

Saved: visits_line.png
Saved: units_bar.png
Saved: study_scatter.png

You should also find three image files created in your working folder.

Run:

python visualization_basics.py

Once you’ve saved all three images with clear labels, you’ve got the core plotting skills down!

Up next: Day 23: Data Cleaning with Pandas β€” handle missing values, fix types, and remove duplicates.

Keep Reading