Day 10: Data Odyssey – How Do We Visualize Data in Python?

Data Odyssey – How Do We Explore Data

Welcome to Day 10: Data Odyssey, our 365-day journey to master data science and artificial intelligence (AI), launched on Shivaratri, February 26, 2025! Yesterday, in Day 9: Data Odyssey – How Do We Work with Data in Python?, we introduced Pandas, a Python library that turned Priya’s POS data into a powerful table. We helped our Delhi café owner load her Monday sales (mean ₹400), group chai vs. samosa totals (₹1400 vs. ₹600), and clean Tuesday’s glitches (₹5000 to ₹500). Pandas made her data structured and actionable. Today, we add a visual spark: How do we visualize data in Python, and how can Priya see her 8-9 AM rush in a graph?

The Magic of Visualization

Visualization turns numbers into pictures—charts, graphs, plots—that reveal what stats alone can’t. On Day 6, we explored data (EDA) and sketched Priya’s sales trends manually—bar charts for hours, line plots for flow. Programming (Day 7) promised automation, and today, we deliver with Matplotlib, Python’s plotting library. Paired with Pandas (Day 9), it’s how data scientists see patterns fast.

For Priya, visuals aren’t decoration—they’re insight. A graph of her sales shows 8-9 AM towering over 11 AM, guiding her hours and stock. Numbers like ₹400 mean fade next to a peak you can see. Day 10: Data Odyssey brings this to life.

Setting Up Matplotlib

Priya’s got Python and Pandas (Days 8-9). Now:

  1. Install Matplotlib:
    • Terminal: pip install matplotlib—free, quick.
    • Done when it says “Successfully installed.”
  2. Import It:
    • Script: import matplotlib.pyplot as plt—“plt” is shorthand.
    • Add import pandas as pd (Day 9).

Priya runs pip install matplotlib, then in IDLE: import matplotlib.pyplot as plt, print(plt.__version__)—sees “3.8.x” or similar. She’s set! Day 10: Data Odyssey starts plotting.

Priya’s Data Recap

Her cleaned Monday data (Day 5, in Pandas from Day 9):

   Hour  Sales   Item
0  7 AM    200   Chai
1  8 AM    500   Chai
2  9 AM    600  Samosa
3 10 AM    400   Chai
4 11 AM    300   Chai

Tuesday:

   Hour  Sales   Item
0  7 AM    150   Chai
1  8 AM    550   Chai
2  9 AM    650  Samosa
3 10 AM    500   Chai
4 11 AM    250   Chai

She wants: Where’s my rush? What sells? Day 9’s stats (mean ₹400, chai ₹1400) helped—now visuals seal it. Day 10: Data Odyssey graphs this.

First Plot: Bar Chart

Let’s plot Monday’s sales by hour:

import pandas as pd
import matplotlib.pyplot as plt

# Data
data = pd.DataFrame({
    "Hour": ["7 AM", "8 AM", "9 AM", "10 AM", "11 AM"],
    "Sales": [200, 500, 600, 400, 300]
})

# Plot
plt.bar(data["Hour"], data["Sales"])
plt.title("Monday Sales by Hour")
plt.xlabel("Hour")
plt.ylabel("Sales (₹)")
plt.show()

Priya runs this—up pops a bar chart! 9 AM’s bar (₹600) towers, 8 AM (₹500) follows, 7 AM (₹200) lags. Her rush is visible. Day 10: Data Odyssey makes it hers.

Line Plot: Daily Flow

For trends over time:

plt.plot(data["Hour"], data["Sales"])
plt.title("Monday Sales Flow")
plt.xlabel("Hour")
plt.ylabel("Sales (₹)")
plt.show()

A line climbs from 7 AM (₹200) to 9 AM (₹600), dips to 11 AM (₹300)—her morning peak shines. Priya sees the arc, not just numbers. Day 10: Data Odyssey adds this curve.

Comparing Days

Monday vs. Tuesday in one plot:

data_mon = pd.DataFrame({
    "Hour": ["7 AM", "8 AM", "9 AM", "10 AM", "11 AM"],
    "Sales": [200, 500, 600, 400, 300]
})
data_tue = pd.DataFrame({
    "Hour": ["7 AM", "8 AM", "9 AM", "10 AM", "11 AM"],
    "Sales": [150, 550, 650, 500, 250]
})

plt.plot(data_mon["Hour"], data_mon["Sales"], label="Monday")
plt.plot(data_tue["Hour"], data_tue["Sales"], label="Tuesday")
plt.title("Sales Flow: Monday vs. Tuesday")
plt.xlabel("Hour")
plt.ylabel("Sales (₹)")
plt.legend()
plt.show()

Two lines—Monday peaks at 9 AM (₹600), Tuesday at 9 AM (₹650), but Tuesday’s steeper (8 AM ₹550 vs. ₹500). Priya spots Tuesday’s edge. Day 10: Data Odyssey layers this.

Pie Chart: Chai vs. Samosa

Day 9 grouped totals—chai ₹1400, samosas ₹600. Visualize it:

totals = pd.Series([1400, 600], index=["Chai", "Samosa"])
plt.pie(totals, labels=totals.index, autopct="%1.1f%%")
plt.title("Monday Item Sales")
plt.show()

A pie—Chai’s 70% (₹1400/2000), Samosa’s 30%. Priya sees chai’s dominance in a slice. Day 10: Data Odyssey rounds this out.

From CSV

Real POS data’s in files. Load monday.csv (Day 9):

data = pd.read_csv("monday.csv")
plt.bar(data["Hour"], data["Sales"])
plt.title("Monday Sales by Hour")
plt.xlabel("Hour")
plt.ylabel("Sales (₹)")
plt.show()

Same bars, less typing—automation! Day 10: Data Odyssey ties Pandas to plots.

Customizing Plots

Make it Priya’s:

  • Color: plt.bar(data[“Hour”], data[“Sales”], color=”teal”)—teal bars.
  • Grid: plt.grid(True)—lines for scale.
  • Size: plt.figure(figsize=(8, 5))—bigger plot. Full script:
plt.figure(figsize=(8, 5))
plt.bar(data["Hour"], data["Sales"], color="teal")
plt.title("Monday Sales by Hour")
plt.xlabel("Hour")
plt.ylabel("Sales (₹)")
plt.grid(True)
plt.show()

Priya’s chart—sharp, clear, hers. Day 10: Data Odyssey adds flair.

Why Visualize?

Graphs beat tables:

  • Instant: 9 AM’s peak jumps out—no scanning ₹600.
  • Compare: Tuesday’s line overtakes Monday’s—obvious.
  • Decide: Chai’s pie slice screams “stock more.”

Priya sees 8-9 AM’s rush, chai’s lead—decisions flow. Day 10: Data Odyssey proves this.

Real-World Visuals

India’s monsoon plots—line graphs of rainfall—warn of floods. Amazon’s bar charts track sales spikes—holiday rushes pop. Priya’s café mirrors this—small scale, big impact. Day 10: Data Odyssey connects her.

Challenges

Plotting trips up:

  • Data Mismatch: Hours vs. sales misaligned—check lengths.
  • Clutter: Too many lines—use legend() wisely.
  • Errors: plt.barr()—typo! Fix to plt.bar.

Priya forgets plt.show()—no plot! She adds it, learns. Day 10: Data Odyssey expects hiccups.

Why This Matters

Visualization turns Priya’s Pandas table into a story—8-9 AM’s rush, chai’s reign, Tuesday’s edge—all at a glance. Without it, she’s buried in numbers; with it, she plans smarter. Scale it: India’s traffic plots cut jams—lives shift with a graph. Day 10: Data Odyssey hands you this art.

Recap Summary

Yesterday, Day 9: Data Odyssey introduced Pandas—Priya’s POS data as a table, mean ₹400, chai ₹1400, cleaned in code. Today, Day 10: Data Odyssey added Matplotlib—plotting her sales (bars, lines, pies) to see her 8-9 AM rush and item trends. It’s her data, visualized.

What’s Next

Tomorrow, in Day 11: Data Odyssey – What is Data Wrangling?, we’ll explore data wrangling: How do we reshape Priya’s data? Combine days? We’ll use Pandas to merge her Monday-Tuesday tables, prepping for bigger analysis. Bring your curiosity, and I’ll see you there!

Authors

Leave a Reply

Your email address will not be published. Required fields are marked *