Python Line Chart Tutorial - Matplotlib Basics
Learn how to create, customize, and interpret line charts in Python using Matplotlib with practical examples, exercises, and beginner-friendly guidance.
Lesson Overview
A line chart is one of the most useful charts for understanding how values change over time. In this lesson, students will learn to prepare data, create line charts using Python's Matplotlib library, customize charts, and interpret trends.
Teaching sequence:
Example → Understand the Data → Create the Chart → Interpret the Results → Modify the Chart → Practice
Main library: Matplotlib
Required knowledge: Basic Python variables, lists, and the print() function.
1. Learning Objectives
After completing this lesson, students will be able to:
- Define a line chart and explain its purpose.
- Identify situations where a line chart is appropriate.
- Understand the X-axis, Y-axis, data points, and connecting lines.
- Prepare a dataset for line chart visualization.
- Create basic line charts using Python and Matplotlib.
- Add titles, labels, markers, gridlines, and legends.
- Customize line colors, styles, sizes, and axis limits.
- Create multiple line charts to compare different datasets.
- Identify increasing, decreasing, constant, and fluctuating trends.
- Interpret information presented in a line chart.
- Save a line chart as an image.
- Select an appropriate chart for a given dataset.
2. Introduction
What is a Line Chart?
A Line Chart is a type of graph that displays information using points connected by straight lines.
It helps us understand how something changes over a period of time.
Real-life example: Daily temperature
Imagine that you record the temperature of your city at different times during the day.
| Time | Temperature |
|---|---|
| 8 AM | 20°C |
| 10 AM | 24°C |
| 12 PM | 30°C |
| 2 PM | 34°C |
| 4 PM | 31°C |
| 6 PM | 27°C |
We can represent this information using a line chart.
How does it work?
A line chart works in three simple steps:
- Select data: Collect values, such as daily temperatures.
- Plot points: Represent each value as a point on the chart.
- Connect points: Join the points using straight lines.
The connected line helps us see whether temperature is increasing, decreasing, or changing.
Why do we use line charts?
Line charts are particularly useful for showing changes over time, such as:
- Monthly student enrollment.
- Daily attendance.
- Monthly business sales.
- Annual examination results.
- Daily weather conditions.
Remember: A line chart helps answer the question: How has something changed over time?
3. Purpose and Applications
The main purpose of a line chart is to display changes and patterns in numerical data.
What information does it communicate?
| Information | Explanation |
|---|---|
| Increasing trend | Values are going up. |
| Decreasing trend | Values are going down. |
| Constant trend | Values remain almost the same. |
| Fluctuation | Values repeatedly increase and decrease. |
| Pattern | A repeated or recognizable change in values. |
| Comparison | Different lines can compare multiple datasets. |
Applications in different fields
Education
Universities can monitor monthly enrollment, daily attendance, and annual examination performance.
Business
Companies can analyze monthly sales, revenue, expenses, and customer growth.
Science
Scientists can monitor experimental measurements and changes in research data over time.
Weather
Meteorologists can study daily temperatures, rainfall, and changes in weather conditions.
Everyday Life
People can track daily walking steps, monthly savings, weight changes, and electricity consumption.
4. Real-life Examples
Let's understand five practical applications.
Example 1: Monthly University Student Enrollment
A university records the number of students enrolled every month.
- X-axis: Months (January to December).
- Y-axis: Number of enrolled students.
- Purpose: Monitor enrollment growth throughout the year.
- Why a line chart? It shows how enrollment changes from one month to another.
Example 2: Daily Student Attendance
A university records the number of students attending classes each day.
- X-axis: Days.
- Y-axis: Number of students present.
- Purpose: Monitor attendance patterns.
- Why a line chart? It identifies days with high or low attendance.
Example 3: Monthly Business Sales
A shop records its monthly sales.
- X-axis: Months.
- Y-axis: Sales amount.
- Purpose: Monitor business performance.
- Why a line chart? It makes increases and decreases in sales easy to identify.
Example 4: Daily Temperature Changes
A weather station records daily temperatures.
- X-axis: Days.
- Y-axis: Temperature in Celsius.
- Purpose: Monitor weather changes.
- Why a line chart? It shows temperature fluctuations over time.
Example 5: Annual University Examination Results
A university records the pass percentage for each year.
- X-axis: Years.
- Y-axis: Pass percentage.
- Purpose: Monitor academic performance.
- Why a line chart? It helps identify long-term improvement or decline.