PYTHON FOR AI • LESSON 4

Matplotlib

Matplotlib is a Python library used to create charts and visualizations. It helps us turn numbers and data into pictures that are much easier to understand.

CORE IDEA

Matplotlib turns data into visual information.

Instead of looking at hundreds of numbers in a DataFrame, we can create a chart and quickly see trends, comparisons, and relationships.

01

What Is Matplotlib?

Matplotlib is one of the most commonly used Python libraries for data visualization.

It allows you to create different types of charts such as line charts, bar charts, histograms, and scatter plots.

Without visualization

January  = 1200
February = 1500
March    = 1800
April    = 1600

You can understand the numbers, but you have to read each value.

With visualization

January  → 1200
February → 1500
March    → 1800
April    → 1600

        ●
      ●   ●
    ●
----------------
Jan Feb Mar Apr

The trend becomes much easier to see.

02

Install Matplotlib

If Matplotlib is not already installed, install it using pip.

pip install matplotlib

If you are working inside a virtual environment, make sure the environment is activated before installing it.

03

Import Matplotlib

The plotting functionality is commonly imported from matplotlib.pyplot.

import matplotlib.pyplot as plt

The plt name is simply a short name that makes the code easier to write.

04

Create Your First Chart

Let's create a simple line chart.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.plot(x, y)

plt.show()

The x values represent the horizontal axis and the y values represent the vertical axis.

x → [1, 2, 3, 4, 5]

y → [10, 20, 15, 30, 25]

Matplotlib connects the points
and displays them as a line chart.
05

Understand plt.plot()

The basic structure is:

plt.plot(x, y)

Think about it like this:

plt.plot(
    horizontal_values,
    vertical_values
)

Matplotlib takes matching values from both lists and creates points on the chart.

x = [1, 2, 3]

y = [10, 20, 15]

Points:

(1, 10)
(2, 20)
(3, 15)

It then connects those points with a line.

06

Add a Title and Axis Labels

A chart should explain what the viewer is looking at. Add a title and labels to make it understandable.

import matplotlib.pyplot as plt

months = [
    "January",
    "February",
    "March",
    "April"
]

sales = [
    1200,
    1500,
    1800,
    1600
]

plt.plot(months, sales)

plt.title("Monthly Sales")

plt.xlabel("Month")
plt.ylabel("Sales")

plt.show()

Now the chart communicates three important things:

Title  → Monthly Sales
X-axis → Month
Y-axis → Sales
07

Add Markers

Markers make individual data points easier to see.

import matplotlib.pyplot as plt

months = [
    "January",
    "February",
    "March",
    "April"
]

sales = [
    1200,
    1500,
    1800,
    1600
]

plt.plot(
    months,
    sales,
    marker="o"
)

plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")

plt.show()

marker="o" tells Matplotlib to display a circular marker at each data point.

08

Add a Grid

A grid can make it easier to read values from a chart.

plt.plot(
    months,
    sales,
    marker="o"
)

plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")

plt.grid(True)

plt.show()

The important function here is:

plt.grid(True)
09

Save a Chart

You don't always want to display a chart only on the screen. You can save it as an image.

plt.plot(months, sales)

plt.title("Monthly Sales")

plt.savefig("monthly-sales.png")

plt.show()

savefig() saves the current figure to a file.

10

Complete Example

Let's put the basic concepts together.

import matplotlib.pyplot as plt

months = [
    "January",
    "February",
    "March",
    "April",
    "May"
]

sales = [
    1200,
    1500,
    1800,
    1600,
    2100
]

plt.plot(
    months,
    sales,
    marker="o"
)

plt.title("Monthly Sales")

plt.xlabel("Month")

plt.ylabel("Sales")

plt.grid(True)

plt.show()

The program performs these steps:

1. Import Matplotlib
        ↓
2. Create the data
        ↓
3. Create the plot
        ↓
4. Add title
        ↓
5. Add axis labels
        ↓
6. Add grid
        ↓
7. Display chart
11

Simple Mental Model

Don't try to memorize every Matplotlib function. Understand the basic workflow first.

Data
 ↓
plt.plot()
 ↓
Title
 ↓
Labels
 ↓
Grid / Markers
 ↓
plt.show()

Once you understand this workflow, the other chart types become much easier because they use the same basic Matplotlib idea.

12

Use Matplotlib With Pandas

This is where Matplotlib becomes especially useful in your Python for AI journey.

Pandas can prepare the data, and Matplotlib can visualize it.

import pandas as pd
import matplotlib.pyplot as plt

sales = pd.DataFrame({
    "Month": [
        "January",
        "February",
        "March",
        "April"
    ],
    "Sales": [
        1200,
        1500,
        1800,
        1600
    ]
})

plt.plot(
    sales["Month"],
    sales["Sales"],
    marker="o"
)

plt.title("Monthly Sales")

plt.xlabel("Month")
plt.ylabel("Sales")

plt.grid(True)

plt.show()

Here Pandas stores and manages the data, while Matplotlib creates the visualization.

Pandas
  ↓
Prepare / organize data
  ↓
Matplotlib
  ↓
Visualize data
13

Why Visualization Matters in AI

Visualization isn't just about making data look nice. It helps you discover patterns that are difficult to notice in raw numbers.

Example 1 — Finding a trend

January  → 100
February → 120
March    → 150
April    → 180
May      → 220

A line chart makes the upward trend immediately obvious.

Example 2 — Finding unusual data

100
105
110
108
107
500
109

A visualization can make the unusual value 500 stand out immediately. This could indicate an outlier or a data problem.

KEY TAKEAWAY

Matplotlib turns data into charts that humans can understand quickly.

The basic workflow is simple: create your data, use a plotting function, add a title and labels, customize the chart when necessary, and display or save it. In the next topics, you will use this foundation to create Line Charts, Bar Charts, Histograms, and Scatter Plots.