Python 8: Data Visualization with matplotlib and seaborn

🎯 Goal:

By the end of this lesson, you’ll be able to:

  • Create simple and beautiful charts using matplotlib and seaborn
  • Visualize sales, product stock, and trends
  • Understand how plots can help make data-driven retail decisions

🧰 1. Getting Started

First, install the libraries (if you haven’t already):

pip install matplotlib seaborn

Then, import them in your notebook:

import matplotlib.pyplot as plt
import seaborn as sns

✅ matplotlib is like the engine

🎨 seaborn adds style and beauty on top


📈 2. Visualize Product Stock Levels

Let’s say you’re running a jewelry store and you want to see what’s in stock.

products = ["Rings", "Necklaces", "Earrings", "Bracelets"]
stock = [10, 5, 12, 8]

plt.figure(figsize=(8, 5))
plt.bar(products, stock)
plt.title("Product Inventory")
plt.xlabel("Product")
plt.ylabel("Stock Level")
plt.show()

This shows which categories are running low.


📊 3. Plotting Sales Data

You have daily sales over a week:

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
sales = [3000, 5000, 4000, 4500, 7000, 9000, 8500]

plt.plot(days, sales, marker='o')
plt.title("Weekly Sales Trend")
plt.ylabel("Sales ($)")
plt.grid(True)
plt.show()

This helps spot peak shopping days (hello, Saturday!).


🧠 4. seaborn: Better Looks with Less Code

Let’s say you have product data in a DataFrame:

import pandas as pd

data = pd.DataFrame({
    "Product": ["Ring", "Necklace", "Earring", "Bracelet"],
    "Price": [2500, 1800, 1200, 900],
    "Stock": [10, 5, 12, 8]
})

sns.barplot(x="Product", y="Stock", data=data)
plt.title("Stock by Product")
plt.show()

Want to show price vs stock?

sns.scatterplot(x="Price", y="Stock", data=data, hue="Product", s=100)
plt.title("Price vs Stock Position")
plt.show()

🧪 5. Practice Time

Try these:

  • Plot your own sales data for 10 days
  • Create a bar chart comparing 3 different employees’ total sales
  • Use seaborn to show stock levels across 6 product types
  • Visualize discounts vs. final prices with a line or scatter plot

✅ Summary

  • matplotlib and seaborn help you see your data clearly
  • Bar charts are great for inventory, line charts for trends, and scatter plots for comparisons
  • Start visualizing early to spot opportunities and problems in retail


Comments

Leave a comment