PYTHON FOR AI • LESSON 4

Line Charts

A line chart is used to show how a value changes across an ordered sequence, such as days, months, years, or other continuous measurements.

CORE IDEA

Line charts help you see trends and changes.

Instead of looking at individual numbers, a line chart connects data points so you can quickly see whether a value is increasing, decreasing, or staying relatively stable.

01

What Is a Line Chart?

A line chart displays data points and connects those points with lines.

It is especially useful when the order of the data matters.

Example: Monthly Sales

January  → 1000
February → 1200
March    → 1500
April    → 1400
May      → 1800

Looking at the numbers tells us the sales values. A line chart makes the overall trend easier to see.

02

When Should You Use a Line Chart?

Use a line chart when your x-axis has a meaningful order.

  • Sales over several months
  • Website visitors over several days
  • Temperature over time
  • Stock prices over time
  • Model accuracy during training

Simple rule

Something changes
       ↓
Order matters
       ↓
Use a line chart
03

Create Your First Line Chart

First import Matplotlib.

import matplotlib.pyplot as plt

Now create the x and y values.

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

sales = [
    1000,
    1200,
    1500,
    1400
]

Then use plt.plot().

plt.plot(months, sales)

plt.show()

The first argument represents the x-axis and the second argument represents the y-axis.

plt.plot(
    x_values,
    y_values
)
04

Understand the Data Points

Matplotlib matches each x value with the corresponding y value.

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

sales = [1000, 1200, 1500]

This creates three points:

("January", 1000)
("February", 1200)
("March", 1500)

Matplotlib then connects these points.

January
   ●
    \
     ● February
       \
        ● March

That connected line is what allows us to see the trend.

05

Add a Title and Labels

A chart should be understandable without needing to guess what the axes mean.

import matplotlib.pyplot as plt

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

sales = [
    1000,
    1200,
    1500,
    1400
]

plt.plot(
    months,
    sales
)

plt.title("Monthly Sales")

plt.xlabel("Month")

plt.ylabel("Sales")

plt.show()

We now have:

Title
  ↓
Monthly Sales

X-axis
  ↓
Month

Y-axis
  ↓
Sales
06

Add Markers

Markers make the individual data points easier to see.

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

plt.show()

The "o" means that Matplotlib should draw a circular marker at each data point.

Without markers:

──────────────

With markers:

●────●────●────●
07

Add a Grid

A grid can make it easier to estimate values from the chart.

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

plt.title("Monthly Sales")

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

plt.grid(True)

plt.show()

The important line is:

plt.grid(True)
08

Customize the Line

Matplotlib allows you to control the appearance of a line.

plt.plot(
    months,
    sales,
    marker="o",
    linestyle="--",
    linewidth=2
)

plt.show()

Here:

marker="o"
    ↓
Circular points

linestyle="--"
    ↓
Dashed line

linewidth=2
    ↓
Line thickness

Don't focus too much on styling at this stage. The important skill is understanding what the data is communicating.

09

Plot Multiple Lines

Sometimes you want to compare two trends on the same chart.

import matplotlib.pyplot as plt

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

product_a = [
    1000,
    1200,
    1500,
    1400
]

product_b = [
    900,
    1100,
    1300,
    1600
]

plt.plot(
    months,
    product_a,
    marker="o",
    label="Product A"
)

plt.plot(
    months,
    product_b,
    marker="o",
    label="Product B"
)

plt.title("Product Sales")

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

plt.legend()

plt.grid(True)

plt.show()

plt.legend() tells Matplotlib to display the labels for the lines.

10

Why Do We Need a Legend?

When there is more than one line, the viewer needs to know which line represents which data.

plt.plot(
    months,
    product_a,
    label="Product A"
)

plt.plot(
    months,
    product_b,
    label="Product B"
)

plt.legend()

The label defines the name of the line and legend() displays those names.

11

Line Charts With Pandas

Since you already learned Pandas, let's combine Pandas and Matplotlib.

import pandas as pd
import matplotlib.pyplot as plt

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

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 holds the data and Matplotlib visualizes it.

Pandas
  ↓
Store and prepare data
  ↓
Matplotlib
  ↓
Create line chart
12

Real-World Example: Website Visitors

Imagine you run a website and want to understand how visitors changed during a week.

import matplotlib.pyplot as plt

days = [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
]

visitors = [
    120,
    150,
    180,
    160,
    220,
    300,
    280
]

plt.plot(
    days,
    visitors,
    marker="o"
)

plt.title("Website Visitors")

plt.xlabel("Day")

plt.ylabel("Visitors")

plt.grid(True)

plt.show()

Looking at the chart, you can quickly identify that visitor numbers increased toward the weekend.

13

Line Charts in AI

Line charts are extremely useful when training Machine Learning and Deep Learning models.

For example, you may want to see how the training loss changes after every epoch.

epochs = [
    1,
    2,
    3,
    4,
    5
]

loss = [
    0.90,
    0.70,
    0.55,
    0.40,
    0.32
]

plt.plot(
    epochs,
    loss,
    marker="o"
)

plt.title("Training Loss")

plt.xlabel("Epoch")

plt.ylabel("Loss")

plt.grid(True)

plt.show()

Here the x-axis represents training progress and the y-axis represents the loss.

Epoch
  ↓
1 → 2 → 3 → 4 → 5

Loss
  ↓
0.90 → 0.70 → 0.55 → 0.40 → 0.32

A decreasing loss can indicate that the model is learning, although you need additional evaluation to determine whether the model is actually generalizing well.

14

Complete Line Chart Example

Here is a clean example combining the concepts you have learned.

import matplotlib.pyplot as plt

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

sales = [
    1000,
    1200,
    1500,
    1400,
    1800,
    2100
]

plt.plot(
    months,
    sales,
    marker="o",
    linewidth=2,
    label="Sales"
)

plt.title("Monthly Sales")

plt.xlabel("Month")

plt.ylabel("Sales")

plt.legend()

plt.grid(True)

plt.show()

The complete workflow is:

Create data
    ↓
plt.plot()
    ↓
Add title
    ↓
Add X-axis label
    ↓
Add Y-axis label
    ↓
Add marker / grid
    ↓
Add legend if needed
    ↓
plt.show()
15

The Most Important Rule

Don't use a line chart simply because you know how to create one.

Use it when the order of the x-axis carries meaning.

Good use

January
February
March
April
May

These values have a natural order, so a line chart makes sense.

Not the best use

India
USA
Germany
Japan

These are separate categories, not a natural sequence. A bar chart is generally better for this kind of comparison.

KEY TAKEAWAY

Line charts are mainly used to understand trends.

Use plt.plot() to create a line chart. Give it x and y values, then add a title and axis labels so the chart is understandable. Markers, grids, and legends can improve readability. Most importantly, use line charts when the order of the x-axis matters, especially for time-based data and AI training metrics.