Module 6 Lesson 8: Matplotlib: Basic Plotting
·Data Science

Module 6 Lesson 8: Matplotlib: Basic Plotting

Visualize your data. Learn the fundamentals of Matplotlib to create line charts, bar graphs, and scatter plots that turn numbers into stories.

Module 6 Lesson 8: Matplotlib: Basic Plotting

Calculations and tables are great, but the human brain is wired to understand pictures. In this lesson, we’ll meet Matplotlib, the "grandfather" of all Python visualization libraries. It’s a powerful but low-level tool that gives you total control over every pixel on your chart.

Lesson Overview

In this lesson, we will cover:

  • The pyplot Module: The simple interface for plotting.
  • Common Plot Types: Line, Bar, and Scatter.
  • Customizing Your Plot: Labels, Titles, and Colors.
  • The show() Command: Bringing your graph to life.

1. Your First Line Plot

The most basic plot is a line graph showing a trend over time.

import matplotlib.pyplot as plt

days = [1, 2, 3, 4, 5]
sales = [10, 25, 15, 30, 45]

plt.plot(days, sales)
plt.show() # This opens a window with your graph

2. Adding Labels and Titles

A graph without labels is just a line. Let's make it professional.

plt.plot(days, sales, color="green", marker="o", linestyle="--")

plt.title("Weekly Sales Performance")
plt.xlabel("Days of the Week")
plt.ylabel("Revenue ($)")

plt.show()

3. Bar Charts and Scatter Plots

Different data needs different pictures.

  • Bar Charts: Best for comparing categories.
  • Scatter Plots: Best for finding relationships between two numbers.
# Bar Chart
products = ["Apple", "Banana", "Cherry"]
prices = [10, 15, 7]
plt.bar(products, prices)
plt.show()

# Scatter Plot
heights = [150, 160, 170, 180]
weights = [50, 60, 70, 80]
plt.scatter(heights, weights)
plt.show()

4. Plotting from Pandas

Pandas has Matplotlib "built-in," so you can plot literally by adding .plot() to your DataFrame!

import pandas as pd
df = pd.DataFrame({"Day": [1, 2, 3], "Score": [10, 20, 15]})

df.plot(x="Day", y="Score", kind="line")
plt.show()

Practice Exercise: The Temperature Graph

  1. Create two lists (or NumPy arrays): hours (1 to 24) and temperature (random values between 15 and 30).
  2. Plot a Line Chart showing the temperature changes throughout the day.
  3. Change the line color to Orange.
  4. Add a Title: "Daily Temperature Cycle".
  5. Add a Grid to the background (hint: look up plt.grid()).

Quick Knowledge Check

  1. Which module within Matplotlib is used for most plotting tasks?
  2. What does plt.show() do?
  3. Why would you use a Bar chart instead of a Line chart?
  4. How do you add a label to the Y-axis?

Key Takeaways

  • Matplotlib is the core library for plotting in Python.
  • Line plots show trends; Bar plots show comparisons; Scatter plots show correlations.
  • Customizing labels and titles is essential for clear communication.
  • Pandas makes it easy to visualize whole tables instantly.

What’s Next?

Matplotlib is powerful, but it takes a lot of code to make it look "beautiful." In Lesson 9, we’ll meet its stylish younger sibling: Seaborn, which creates stunning, modern statistical graphics with much less effort!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn