Gen AI Developer Week 1 — Day 4
Visualization is a powerful way to understand and communicate data, and Matplotlib is the go-to Python library for creating stunning and informative visualizations. From simple line plots to complex multi-plot figures, Matplotlib provides the flexibility and tools to bring data to life. As part of my Generative AI Developer Week journey, this article explores how Matplotlib empowers developers to analyze trends, share insights, and prepare visualizations that complement AI and machine learning projects.
Let’s first install pandas using the below pip command installation.
# Install matplotlib using the below command
pip install matplotlib
Now lets import matplotlib and get started for this day!
import matplotlib.pyplot as plt
Key Concepts — Basic Plot
# Basic Plot
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
plt.plot(x,y)
plt.title("Basic Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Key Concepts — Scatter Plot
# Scatter Plot
x = [5, 7, 8, 7, 2, 17, 2, 9]
y = [99, 86, 87, 88, 100, 86, 103, 87]
plt.scatter(x, y, color='red')
plt.title("Scatter Plot Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Key Concepts — Bar Plot
# Bar Chart
categories = ["A", "B", "C", "D"]
values = [3, 7, 8, 5]
plt.bar(categories, values, color='blue')
plt.title("Bar Chart Example")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()
Key Concepts — Histogram Plot
# Histogram Plot
data = [22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31, 27]
plt.hist(data, bins=5, color='green', edgecolor='black')
plt.title("Histogram")
plt.xlabel("Bins")
plt.ylabel("Frequency")
plt.show()
Key Concepts — Pie Chart Plot
# Pie Chart Plot
sizes = [15, 30, 45, 10]
labels = ["A", "B", "C", "D"]
colors = ["gold", "yellowgreen", "lightcoral", "lightskyblue"]
plt.pie(sizes, labels=labels, colors=colors, autopct="%1.1f%%")
plt.title("Pie Chart Example")
plt.show()
Task 1: Line Plot
# Create a line plot showing the trend of monthly expenses for 6 months:
# [200, 250, 270, 300, 320, 310].
expenses = [200, 250, 270, 300, 320, 310]
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
plt.plot(months, expenses, marker="o")
plt.title("Monthly Expenses")
plt.xlabel("Months")
plt.ylabel("Expenses ($)")
plt.show()
Task 2: Scatter Plot
# Visualize the relationship between students study hours and their test scores:
# Hours: [2, 3, 4, 5, 6, 7]
# Scores: [50, 55, 65, 70, 80, 85]
hours = [2, 3, 4, 5, 6, 7]
scores = [50, 55, 65, 70, 80, 85]
plt.scatter(hours, scores, color = "purple")
plt.title("Study Hours vs Test Scores")
plt.xlabel("Study Hours")
plt.ylabel("Test Scores")
plt.show()
Task 3: Bar Chart
# Create a bar chart showing sales data for four products:
# Products: ["Product A", "Product B", "Product C", "Product D"]
# Sales: [150, 200, 250, 180]
products = ["Product A", "Product B", "Product C", "Product D"]
sales = [150, 200, 250, 180]
plt.bar(products, sales, color="orange")
plt.title("Product Sales")
plt.xlabel("Products")
plt.ylabel("Sales ($)")
plt.show()
Task 4: Histogram
# Generate a histogram for ages of a group:
# Ages: [21, 22, 23, 24, 25, 22, 21, 23, 24, 25, 23, 24, 22, 21].
ages = [21, 22, 23, 24, 25, 22, 21, 23, 24, 25, 23, 24, 22, 21]
plt.hist(ages, bins=5, color="red", edgecolor="black")
plt.title("Age Distribution")
plt.xlabel("Age Groups")
plt.ylabel("Frequency")
plt.show()
Task 5: Pie Chart
# Create a pie chart showing market share percentages of 4 companies:
# Companies: ["Company A", "Company B", "Company C", "Company D"]
# Shares: [40, 25, 20, 15]
shares = [40, 25, 20, 15]
companies = ["Company A", "Company B", "Company C", "Company D"]
colors = ["gold", "blue", "green", "red"]
plt.pie(shares, labels=companies, colors=colors, autopct="%1.1f%%")
plt.title("Market Share")
plt.show()
Practice Task Challenges
- Use real-world data (e.g., from a CSV file) to create at least two visualizations.
- Combine multiple plots into a single figure using
plt.subplot
.
Happy Learning!😊.. For any questions or support, feel free to message me on LinkedIn.