Documentation and resources for Basic Line.
Complete Beginner-Friendly Lesson · Data Visualization · Python
Beginner Level
15 Sections
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.
After completing this lesson, students will be able to:
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.
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.
blended-learning.resources.wjec.co.uk
A line chart works in three simple steps:
The connected line helps us see whether temperature is increasing, decreasing, or changing.
Line charts are particularly useful for showing changes over time, such as:
Remember: A line chart helps answer the question: How has something changed over time?
The main purpose of a line chart is to display changes and patterns in numerical data.
| 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. |
Education
Universities can monitor monthly enrollment, daily attendance, and annual examination performance.
Business
Companies can analyze monthly sales, revenue, expenses, and customer growth.
Innovating Biopharmaceutical Manufacturing: Timegate’s Real-Time Solutions Transform Process Control
Science
Scientists can monitor experimental measurements and changes in research data over time.
NETHERLANDS-WEATHER-CLIMATE-HEAT
Weather
Meteorologists can study daily temperatures, rainfall, and changes in weather conditions.
| [[100+] Smartwatch Backgrounds | Wallpapers.com](https://images.openai.com/static-rsc-4/GkFWqprbG7hVzJLaJRAvMCnVqVUtseVv2K20h3wUug0fMPxzTDcm89qE-qrbA6gYxj_B_ZsniG6E5muxbVTKNWqNJVC9XW94m8vF5dceWqkzLHcom-630RSobzi4W0-l61CAVzCE-IEsb1g-cWyhj-MWTu59QNoa9hSXGTdCv_I?purpose=inline) |
Everyday Life
People can track daily walking steps, monthly savings, weight changes, and electricity consumption.
Let’s understand five practical applications.
A university records the number of students enrolled every month.
| [Statistics | Mechanical Engineering](https://images.openai.com/static-rsc-4/e9ziPJzDlgUZc6Iym-5cpsNtoHU-A2-wtXcxnqM9OTojURIj7fTgqCDZ_oLo-TEXplfoJeB9tkhMH8Qww1KWbl0swC4WLW-lKHcY4bgWTklxGUjs2tp_v3e2TFdGeo17RllxWDw-28-1g65pqpGJ0ZG9wiaguwML-ztrAE8AgzE?purpose=inline) |
A university records the number of students attending classes each day.
A shop records its monthly sales.
A weather station records daily temperatures.
A university records the pass percentage for each year.
Before creating a chart, we need to understand the data.
We will use a university student enrollment dataset.
| Month | Students Enrolled |
|---|---|
| January | 50 |
| February | 65 |
| March | 60 |
| April | 80 |
| May | 75 |
| June | 100 |
| July | 110 |
| August | 95 |
| September | 120 |
| October | 130 |
| November | 125 |
| December | 150 |
Consider the following example:
| Month | Students Enrolled |
|---|---|
| January | 50 |
| February | 65 |
A line chart normally has two axes.
Horizontal axis
Month
January, February, March, and so on.
Vertical axis
Number of Students
50, 65, 60, 80, and so on.
Each month has a corresponding enrollment value.
For example:
| Month | Students | Chart point |
|---|---|---|
| January | 50 | January → 50 |
| February | 65 | February → 65 |
| March | 60 | March → 60 |
| April | 80 | April → 80 |
The chart places each point according to its month and numerical value.
Then, the points are connected with lines.
Important: The X-axis shows the order or position of the data, while the Y-axis shows the corresponding numerical values.
Use a line chart when:
Line charts work best when the X-axis follows a meaningful sequence.
For example:
January → February → March → April
This order makes sense because months follow a natural sequence.
However, consider these unrelated categories:
Computer Science → English → Physics → Biology
Connecting these subjects with a line may suggest a continuous progression that does not actually exist.
A bar chart would usually be more appropriate for comparing unrelated subjects.
Avoid line charts when:
| Feature | Line Chart | Bar Chart | Area Chart | Scatter Plot |
|---|---|---|---|---|
| Purpose | Show trends | Compare values | Show trends and magnitude | Show relationships |
| Suitable data | Ordered data | Categories | Ordered data | Paired numerical values |
| Best use case | Monthly sales | Sales by product | Population growth over time | Height vs weight |
| Main difference | Connects points | Uses rectangular bars | Fills area below line | Uses unconnected points |
Practical examples:
A line chart contains several important components.
| Component | Explanation |
|---|---|
| Chart Title | Describes what the chart represents. |
| X-axis | Horizontal axis, usually representing time or ordered categories. |
| Y-axis | Vertical axis, representing numerical values. |
| X-axis Values | Values displayed along the horizontal axis. |
| Y-axis Values | Numerical scale displayed along the vertical axis. |
| Data Points | Individual points representing observations. |
| Connecting Lines | Lines joining data points. |
| Axis Labels | Names describing each axis. |
| Tick Marks | Small marks showing positions on an axis. |
| Gridlines | Reference lines that help read values. |
| Legend | Identifies different lines when multiple datasets are displayed. |
| [The Register | The HCPC](https://images.openai.com/static-rsc-4/rn6tJ4epi3uGjR5K5cWeQqkHWtFrzHHVye_Xy-STxKAPExejgZH9h9m0_AsvJAIwciHRGxy9_48kuoOBiKu-V8Mm_vkV26i94qSp632Yd70ZHLJyeVpMkfSeMxdfBwHd14mSLIEZ7bh3b6YIgkAG3UKTIfUUtcC1hL0bZCPmSKY?purpose=inline) |
Increasing Trend
Values move upward.
Decreasing Trend
Values move downward.
Free Tools & Spare Parts STL Download - шильдик ладаby ugadaiugadaev
Constant Trend
Values remain unchanged.
Fluctuating Trend
Values repeatedly rise and fall.
A multiple line chart displays two or more datasets on the same chart.
For example, we can compare monthly enrollment in:
Each department gets a separate line and a legend identifies each line.
| [UC Berkeley Data: Freshman Admissions for the EECS and L&S CS Majors | Ronald Kwan](https://images.openai.com/static-rsc-4/YglpzMYhS17W1THrB_P2R9yklbgXSB6fqhhFakokmH1rfgMdBv4xE5d9j-Spr8XEnG9xw9eWMzwi1AgB3DUpXSGiih9MWrh8EHCOfUcBseq-VWBRriajov4cd7lodBX97rpqEtI9p6hKVGMfl3whTTiCu3gxt9DiEOFDMl70lOI?purpose=inline) |
Remember: Use different colors or markers to make multiple lines easy to distinguish.
Now we will create our first line chart using Python and Matplotlib.
We will use the university enrollment dataset introduced earlier.
Matplotlib is a Python library used to create charts and graphs.
Open your terminal or command prompt and execute:
pip install matplotlib
Wait until the installation is completed.
Note: If you are using Jupyter Notebook, you can run !pip install matplotlib in a notebook cell.
Before using Matplotlib, we need to import it.
import matplotlib.pyplot as plt
Understand the code:
import loads a Python library.matplotlib is the library name.pyplot provides functions for creating charts.as plt gives us a short name to use instead of writing matplotlib.pyplot every time.For example, we can write:
plt.show()
Instead of writing the full library name.
We will store the months and enrollment values in two Python lists.
months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]students = [50, 65, 60, 80, 75, 100, 110, 95, 120, 130, 125, 150]
Understand the code:
months stores the names of the months.students stores the number of enrolled students.For example:
| Position | Month | Students |
|---|---|---|
| 0 | January | 50 |
| 1 | February | 65 |
| 2 | March | 60 |
| 3 | April | 80 |
Important: Both lists must contain the same number of items.
We will use plt.plot() to create our first line chart.
import matplotlib.pyplot as pltmonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]students = [50, 65, 60, 80, 75, 100, 110, 95, 120, 130, 125, 150]plt.plot(months, students)plt.show()
| [The Complete Guide to Line Charts with Matplotlib: Unleash the Power of Visual Storytelling | by Datasans | Medium](https://images.openai.com/static-rsc-4/nuuOsKEiPJglSDkszoUFoxJs2Qjt_xBpB_mgtC5p8Cmppo6K-_MMjprr3xoi5aIpORrTe3YI7spgj8qnMmc4Bu9fx8rK5kG9eb0pXboOVwkTl3imt2qclM3zcnpr-SqEZnBtI8gBjrtZhPGR_D-vENlkS5XP97T1lmF2eCi3_VM?purpose=inline) |
Understand plt.plot():
plt.plot(months, students)
| Part | Meaning |
|---|---|
plt |
Matplotlib pyplot |
plot() |
Creates a line chart |
months |
X-axis data |
students |
Y-axis data |
What happens?
plt.show() displays the chart.A chart title tells viewers what the chart represents.
Use plt.title().
plt.plot(months, students)plt.title("Monthly Student Enrollment")plt.show()
| [Student enrolment and result detail | COLLEGE OF BUSINESS](https://images.openai.com/static-rsc-4/F3kbnvG4gONCF394eEVWBLpgM7vFZbV1X-6aFQVhmqCuW6ik1rOjB0X4VBRoWpI_Q3A1mNsf9R4sHx4saYMC-VD1poUml5A-Rm6cRvO00R4BQruax8kqVrIK0xFqyzarM0Ef4-XDt3MAxSYf5tPNPxnlZ4xSjcvniyYaZWvhngc?purpose=inline) |
We should describe what each axis represents.
Use:
plt.xlabel() for the horizontal axis.plt.ylabel() for the vertical axis.plt.plot(months, students)plt.title("Monthly Student Enrollment")plt.xlabel("Months")plt.ylabel("Number of Students")plt.show()
Expected chart information:
The function plt.show() displays the completed chart.
It is normally placed at the end of the chart code.
plt.show()
Without this command, a chart may not appear in some Python environments.
A line connects data points, but we may also want to display the individual points clearly.
We can use the marker parameter.
plt.plot(months, students, marker="o")plt.title("Monthly Student Enrollment")plt.xlabel("Months")plt.ylabel("Number of Students")plt.show()
[Excel] 꺾은선 차트가 삐죽삐죽하다면? 부드러운 곡선으로 차트 꾸미기
Here:
marker="o" displays circular markers.Other marker examples:
| Marker | Appearance |
|---|---|
"o" |
Circle |
"s" |
Square |
"^" |
Triangle |
"D" |
Diamond |
"*" |
Star |
"x" |
Cross |
We can customize the appearance of the line.
plt.plot( months, students, color="green", linestyle="--", marker="o")plt.title("Monthly Student Enrollment")plt.xlabel("Months")plt.ylabel("Number of Students")plt.show()
Important parameters:
| Parameter | Purpose |
|---|---|
color |
Changes line color. |
linestyle |
Changes line appearance. |
marker |
Displays data points. |
Common line styles:
| Style | Appearance |
|---|---|
"-" |
Solid line |
"--" |
Dashed line |
":" |
Dotted line |
"-." |
Dash-dot line |
Common colors include:
"red", "blue", "green", "orange", "black" and "purple".
Gridlines make it easier to read numerical values.
Use plt.grid().
plt.plot(months, students, marker="o")plt.title("Monthly Student Enrollment")plt.xlabel("Months")plt.ylabel("Number of Students")plt.grid(True)plt.show()
Python Charts - Saving High-Resolution Figures in Seaborn
Here:
plt.grid(True) displays gridlines.A legend identifies the data shown by a line.
We can use label inside plt.plot() and plt.legend() to display it.
plt.plot( months, students, marker="o", color="blue", label="University Enrollment")plt.title("Monthly Student Enrollment")plt.xlabel("Months")plt.ylabel("Number of Students")plt.legend()plt.grid(True)plt.show()
Understand:
label gives a name to the line.plt.legend() displays that name on the chart.A legend becomes especially important when a chart contains multiple lines.
Sometimes we want to show the exact numerical value beside each point.
We can use plt.annotate().
plt.plot(months, students, marker="o")for i in range(len(months)): plt.annotate( students[i], (months[i], students[i]) )plt.title("Monthly Student Enrollment")plt.xlabel("Months")plt.ylabel("Number of Students")plt.grid(True)plt.show()
Understand the new code:
len(months) returns the number of months.range() generates positions from zero to eleven.for repeats the annotation operation for every month.students[i] gets the enrollment value at position i.(months[i], students[i]) identifies the location of the point.Result: Every data point displays its corresponding enrollment value.
Now combine the concepts learned in this section.
import matplotlib.pyplot as plt# Step 1: Prepare datamonths = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]students = [50, 65, 60, 80, 75, 100, 110, 95, 120, 130, 125, 150]# Step 2: Create line chartplt.plot( months, students, color="blue", linestyle="-", linewidth=2, marker="o", markersize=7, label="Student Enrollment")# Step 3: Add chart informationplt.title("Monthly Student Enrollment")plt.xlabel("Month")plt.ylabel("Number of Students")# Step 4: Add data labelsfor i in range(len(months)): plt.annotate( students[i], (months[i], students[i]), textcoords="offset points", xytext=(0, 8),
Yearly Overview: Previous Overviews: Office of Enrollment Management: Indiana University Bloomington
What have we learned?
We have created a line chart with:
In this section, we will learn how to modify an existing line chart.
The objective is to understand how changing individual parameters affects the chart.
Use plt.title().
plt.title("University Enrollment Report")
You can replace the title with any meaningful description.
Use the color parameter.
plt.plot(months, students, color="red")
Other options include:
color="blue"color="green"color="orange"color="purple"
Use linewidth.
plt.plot(months, students, linewidth=3)
linewidth=1: Thin line.linewidth=2: Medium line.linewidth=3: Thick line.Use linestyle.
plt.plot(months, students, linestyle="--")
Examples:
plt.plot(months, students, linestyle="-")plt.plot(months, students, linestyle="--")plt.plot(months, students, linestyle=":")plt.plot(months, students, linestyle="-.")
Use marker.
plt.plot(months, students, marker="o")
This displays a circular marker at each data point.
Use markersize.
plt.plot( months, students, marker="o", markersize=10)
Larger values make the markers bigger.
Matplotlib allows us to customize the marker’s face and border colors.
plt.plot( months, students, marker="o", color="blue", markerfacecolor="yellow", markeredgecolor="red", markersize=8)plt.show()
| Parameter | Purpose |
|---|---|
color |
Connecting line color |
markerfacecolor |
Inner marker color |
markeredgecolor |
Marker border color |
markersize |
Marker size |
Use plt.figure(figsize=(width, height)).
plt.figure(figsize=(12, 5))plt.plot(months, students, marker="o")plt.show()
Here:
12 represents the figure width in inches.5 represents the figure height in inches.A larger figure provides more space for the chart.
We can change the font size of the title and axis labels.
plt.plot(months, students, marker="o")plt.title("Monthly Enrollment", fontsize=18)plt.xlabel("Month", fontsize=12)plt.ylabel("Students", fontsize=12)plt.show()
Use plt.annotate() to display values near data points.
plt.plot(months, students, marker="o")for i in range(len(months)): plt.annotate( students[i], (months[i], students[i]), xytext=(0, 8), textcoords="offset points", ha="center" )plt.show()
The xytext parameter controls the position of the annotation relative to the point.
plt.grid(True)
You can also customize gridline appearance.
plt.grid( True, linestyle="--", alpha=0.5)
Here:
linestyle="--" displays dashed gridlines.alpha=0.5 controls transparency.Ticks are the values displayed along the axes.
For example:
plt.plot(months, students, marker="o")plt.yticks(range(0, 181, 20))plt.show()
This displays Y-axis values at intervals of 20:
0, 20, 40, 60, 80, ... 180
We can also customize the X-axis ticks.
plt.xticks(rotation=45)
Long category names may overlap.
Use:
plt.xticks(rotation=45)
For example, month names will be displayed at a 45-degree angle.
plt.plot( months, students, label="Enrollment")plt.legend()
We can also change the legend position:
plt.legend(loc="upper left")
Axis limits control the range displayed on the chart.
Use:
plt.xlim() for the horizontal axis.plt.ylim() for the vertical axis.Example:
plt.plot(months, students, marker="o")plt.ylim(0, 180)plt.show()
This sets the Y-axis range from 0 to 180.
Important: Choose limits carefully. An inappropriate scale can make small changes appear much larger than they really are.
Use plt.savefig().
plt.plot(months, students, marker="o")plt.title("Monthly Student Enrollment")plt.savefig( "student_enrollment.png", dpi=300, bbox_inches="tight")plt.show()
Understand the parameters:
"student_enrollment.png" is the image filename.dpi=300 controls image resolution.bbox_inches="tight" reduces unnecessary empty space around the chart.The image is saved in the current working directory.
Now we will compare enrollment in two university departments.
Dataset
| Month | Computer Science | Information Technology |
|---|---|---|
| Jan | 50 | 40 |
| Feb | 55 | 45 |
| Mar | 60 | 50 |
| Apr | 65 | 55 |
| May | 70 | 60 |
| Jun | 75 | 70 |
| Jul | 80 | 75 |
| Aug | 85 | 80 |
| Sep | 90 | 85 |
| Oct | 95 | 90 |
| Nov | 100 | 95 |
| Dec | 110 | 100 |
Complete Python code
import matplotlib.pyplot as pltmonths = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]cs_students = [50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 110]it_students = [40, 45, 50, 55, 60, 70, 75, 80, 85, 90, 95, 100]plt.figure(figsize=(10, 5))plt.plot( months, cs_students, color="blue", marker="o", label="Computer Science")plt.plot( months, it_students, color="green", marker="s", label="Information Technology")plt.title("Monthly Department Enrollment")plt.xlabel("Month")plt.ylabel("Number of Students")plt.grid(True, alpha=0.3)plt.legend()plt.tight_layout()plt.show()
What does this chart show?
We can display multiple line styles to understand how Matplotlib represents different patterns.
import matplotlib.pyplot as pltmonths = ["Jan", "Feb", "Mar", "Apr", "May"]a = [20, 30, 40, 50, 60]b = [25, 35, 45, 55, 65]c = [30, 40, 50, 60, 70]d = [35, 45, 55, 65, 75]plt.plot(months, a, linestyle="-", label="Solid")plt.plot(months, b, linestyle="--", label="Dashed")plt.plot(months, c, linestyle=":", label="Dotted")plt.plot(months, d, linestyle="-.", label="Dash-dot")plt.title("Different Line Styles")plt.xlabel("Month")plt.ylabel("Values")plt.legend()plt.grid(True)plt.show()
请在同一张图中画出“实线、点线、虚线、双线”这四种线_python线的类型 有哪些对应代码-CSDN博客
Markers make individual data points easier to identify.
import matplotlib.pyplot as pltdays = ["Mon", "Tue", "Wed", "Thu", "Fri"]attendance = [80, 85, 78, 90, 95]plt.plot( days, attendance, marker="o", markersize=10, color="purple", linewidth=2)plt.title("Weekly Student Attendance")plt.xlabel("Days")plt.ylabel("Number of Students")plt.grid(True)plt.show()
ANALYZING AND INTERPRETING GRAPHS OF PRIMARY DATA.pptx
A standard line chart normally uses:
The X-axis represents the sequence, such as months or days.
The Y-axis represents numerical measurements, such as enrollment or temperature.
Can we make a horizontal line chart?
Yes. We can swap the axes using plt.plot().
import matplotlib.pyplot as pltmonths = [1, 2, 3, 4, 5]students = [50, 65, 60, 80, 75]plt.plot(students, months, marker="o")plt.title("Horizontal Orientation Example")plt.xlabel("Number of Students")plt.ylabel("Month Number")plt.grid(True)plt.show()
However, this changes the meaning of the axes. It is not the usual way of displaying time-based line charts.
Remember: Use the horizontal axis for the ordered sequence and the vertical axis for the measured values in a standard time-series line chart.
Creating a chart is only one part of data visualization.
We must also understand what the chart tells us.
Let’s examine our original student enrollment dataset.
| Month | Students |
|---|---|
| January | 50 |
| February | 65 |
| March | 60 |
| April | 80 |
| May | 75 |
| June | 100 |
| July | 110 |
| August | 95 |
| September | 120 |
| October | 130 |
| November | 125 |
| December | 150 |
Figure 1 - from Beliefs and Attitudes toward the English
Answer: December
December has 150 enrolled students, which is the highest value in the dataset.
Answer: January
January has 50 enrolled students.
Enrollment increased during these transitions:
Enrollment decreased during:
Highest enrollment = 150
Lowest enrollment = 50
Difference:
\[ 150-50=100 \]
Answer: 100 students.
Let’s calculate the changes between consecutive months.
| Period | Change |
|---|---|
| Jan–Feb | +15 |
| Feb–Mar | -5 |
| Mar–Apr | +20 |
| Apr–May | -5 |
| May–Jun | +25 |
| Jun–Jul | +10 |
| Jul–Aug | -15 |
| Aug–Sep | +25 |
| Sep–Oct | +10 |
| Oct–Nov | -5 |
| Nov–Dec | +25 |
Answer: May–June, August–September, and November–December share the greatest increase of 25 students.
The overall trend is increasing.
Enrollment starts at 50 students in January and reaches 150 students in December.
Although there are some decreases during the year, the general direction is upward.
| Pattern | How to identify it | Example |
|---|---|---|
| Upward trend | Line moves upward | Sales increase |
| Downward trend | Line moves downward | Temperature decreases |
| Stable period | Line remains nearly horizontal | Attendance stays similar |
| Sudden change | Line moves sharply | Enrollment increases quickly |
| Peak | Highest point | Highest monthly sales |
| Lowest point | Lowest point | Lowest daily attendance |
| Fluctuation | Line repeatedly rises and falls | Daily temperature changes |
Practice reading charts: Always look at the overall direction first, then examine individual increases and decreases.
Students should understand common mistakes before using line charts in real projects.
| Mistake | Why it is a problem | How to avoid it |
|---|---|---|
| Using line charts for unrelated categories | Suggests a relationship between categories | Use a bar chart instead. |
| Incorrect data sequence | Creates misleading trends | Arrange data in the correct order. |
| Missing chart title | Viewers cannot easily understand the chart | Add a meaningful title. |
| Missing axis labels | Axis meanings are unclear | Label both axes. |
| Too many lines | Makes the chart difficult to read | Show fewer relevant datasets. |
| Too many colors | Creates visual confusion | Use a limited color palette. |
| Misleading axis scales | Exaggerates or hides changes | Choose appropriate axis limits. |
| Inappropriate markers | Makes the chart crowded | Use simple markers. |
| Unnecessary connecting lines | Suggests a continuous relationship | Use scatter plots for unrelated observations. |
| Too many data labels | Makes the chart overcrowded | Label only important points when necessary. |
Important lesson: A chart should make information easier to understand, not create confusion.
Let’s compare line charts with three other common visualization types.
| [Creating Matplotlib Bar Charts: A Step-by-Step Guide | LabEx](https://images.openai.com/static-rsc-4/ZmOycWA3jmfQJO0DHE37hZ95eXPblo_mClc8A1z1iMmP_YNQx-uHUszTN3DcQ51iEbidpC-Ef3I3rnqOx1UKaykXUIMxW-5vYMPLJZDg7MaPefu7srhkujTI3rkEgoAU3lvpgUYE502jgfARhEr-_QT7BCtGAmoiOwq12kxVEz8?purpose=inline) |
1. Bar Chart
Used to compare numerical values across different categories.
Example: Comparing the number of students in Computer Science, IT, Mathematics, and Physics.
| [Area Graph | Create Area Charts with Python - Plotivy](https://images.openai.com/static-rsc-4/2eKMR96AMQBkovVAMRiBB7VVPpZJpOLllDnaRYERicLdmhFIjNYbv6WewpeDjHCuV-qAg9KXlEeMpJkPuKDP17dWtAoUmrIJCmywyHz_6pH0ic_LCNVpEYFdXhbqe-InpkTwQebf9tsc4HTzkUBhbYY-SziD8EjJdTywgUBxF8M?purpose=inline) |
2. Area Chart
Used to show changes over time while emphasizing the magnitude of values through a filled area.
Example: Showing monthly electricity consumption.
Regression Analysis - DEV Community
3. Scatter Plot
Used to examine relationships between two numerical variables.
Example: Studying the relationship between study hours and examination marks.
| Feature | Line Chart | Bar Chart | Area Chart | Scatter Plot |
|---|---|---|---|---|
| Purpose | Show changes and trends | Compare categories | Show magnitude over time | Show relationships |
| Suitable data | Ordered values | Categorical values | Ordered numerical values | Two numerical variables |
| Best use case | Monthly enrollment | Students by department | Monthly revenue | Study hours vs marks |
| Main difference | Connects data points | Uses bars | Fills area under line | Displays separate points |
| Your requirement | Recommended chart |
|---|---|
| Compare monthly sales | Line Chart |
| Compare sales of products | Bar Chart |
| Show revenue growth and magnitude | Area Chart |
| Compare study hours and marks | Scatter Plot |
| Track daily attendance | Line Chart |
| Compare students in different departments | Bar Chart |
| Show temperature changes | Line Chart |
Remember these simple rules:
Your turn to practice
A weather station records the average monthly temperature of a city.
Use the following dataset.
| Month | Average Temperature (°C) |
|---|---|
| January | 12 |
| February | 15 |
| March | 20 |
| April | 26 |
| May | 32 |
| June | 36 |
| July | 35 |
| August | 33 |
| September | 30 |
| October | 25 |
| November | 19 |
| December | 14 |
Identify:
Create two lists:
months = [...]temperature = [...]
Fill these lists using the dataset.
Use:
plt.plot(months, temperature)
Display the chart.
Your chart should contain:
Use circular markers to show individual temperature values.
Hint:
marker="o"
Change the line color to red.
Hint:
color="red"
Display gridlines to make temperature values easier to read.
Hint:
plt.grid(True)
Answer these questions:
Your learning challenge
Complete the Python program yourself before looking for a solution.
Prepare the two Python lists.
Create the basic line chart.
Add title and axis labels.
Add markers, color, and gridlines.
Interpret the temperature trend.
0 of 5 tasks completed
Now complete the following exercises independently.
The exercises are arranged from beginner to advanced level.
Scenario
A shop owner wants to monitor monthly sales.
Use the following dataset:
| Month | Sales (PKR) |
|---|---|
| January | 20,000 |
| February | 25,000 |
| March | 22,000 |
| April | 30,000 |
| May | 35,000 |
| June | 40,000 |
| July | 38,000 |
| August | 45,000 |
| September | 50,000 |
| October | 48,000 |
| November | 55,000 |
| December | 65,000 |
Requirements
plt.plot().Questions
Scenario
A teacher records the attendance of students over 15 working days.
Use the following dataset:
| Day | Students Present |
|---|---|
| Day 1 | 35 |
| Day 2 | 38 |
| Day 3 | 36 |
| Day 4 | 40 |
| Day 5 | 42 |
| Day 6 | 39 |
| Day 7 | 43 |
| Day 8 | 45 |
| Day 9 | 41 |
| Day 10 | 44 |
| Day 11 | 46 |
| Day 12 | 48 |
| Day 13 | 45 |
| Day 14 | 47 |
| Day 15 | 50 |
Requirements
Questions
Scenario
A university wants to compare enrollment in three departments over 12 months.
Use the following dataset:
| Month | Computer Science | IT | Mathematics |
|---|---|---|---|
| Jan | 50 | 40 | 30 |
| Feb | 55 | 45 | 32 |
| Mar | 60 | 48 | 35 |
| Apr | 65 | 55 | 38 |
| May | 70 | 60 | 40 |
| Jun | 75 | 65 | 45 |
| Jul | 80 | 70 | 48 |
| Aug | 85 | 75 | 50 |
| Sep | 90 | 80 | 55 |
| Oct | 95 | 85 | 58 |
| Nov | 100 | 90 | 60 |
| Dec | 110 | 100 | 65 |
Requirements
plt.show().Questions
This section checks your understanding of line charts and Python Matplotlib.
Select the correct answer for each question.
Question 1. What is the main purpose of a line chart?
A. To display text information.
B. To show changes and trends over time.
C. To display images.
D. To store numerical values.
Question 2. Which Matplotlib function is used to create a line chart?
A. plt.bar()
B. plt.scatter()
C. plt.plot()
D. plt.pie()
Question 3. Which parameter is used to display circular markers?
A. color="o"
B. marker="o"
C. linestyle="o"
D. markersize="o"
Question 4. Which function is used to display gridlines?
A. plt.grid()
B. plt.title()
C. plt.legend()
D. plt.xlabel()
Question 5. Which chart is generally more suitable for comparing sales of different products?
A. Line Chart
B. Area Chart
C. Bar Chart
D. Scatter Plot
Predict the output of each Python program.
Question 6. Understanding a basic line chart
import matplotlib.pyplot as pltmonths = ["Jan", "Feb", "Mar"]sales = [20, 40, 30]plt.plot(months, sales)plt.show()
Answer:
Question 7. Understanding markers and gridlines
import matplotlib.pyplot as pltdays = [1, 2, 3, 4]attendance = [30, 35, 40, 45]plt.plot(days, attendance, marker="o")plt.grid(True)plt.show()
Answer:
marker="o" do?plt.grid(True) do?Question 8. Understanding multiple lines
import matplotlib.pyplot as pltmonths = ["Jan", "Feb", "Mar"]a = [10, 20, 30]b = [30, 20, 10]plt.plot(months, a, label="Department A")plt.plot(months, b, label="Department B")plt.legend()plt.show()
Answer:
plt.legend()?Modify the given programs according to the requirements.
Question 9. Change line appearance
Given code:
plt.plot(months, students)
Modify it to:
Question 10. Add chart information
Given code:
plt.plot(months, sales)plt.show()
Modify it to:
Question 11. Add a legend and save the chart
Given code:
plt.plot(months, cs_students, label="Computer Science")plt.plot(months, it_students, label="Information Technology")
Modify it to:
department_enrollment.png.Question 12. Monthly Electricity Consumption
A household records electricity consumption over six months.
| Month | Units Consumed |
|---|---|
| January | 250 |
| February | 280 |
| March | 260 |
| April | 300 |
| May | 320 |
| June | 350 |
Create a line chart that:
Question 13. Compare Two Departments
A university wants to compare student enrollment in two departments.
| Month | Computer Science | Mathematics |
|---|---|---|
| January | 40 | 30 |
| February | 45 | 35 |
| March | 50 | 38 |
| April | 55 | 42 |
| May | 65 | 45 |
| June | 70 | 50 |
Create a multiple line chart that:
A line chart is a graph that displays numerical information using data points connected by lines.
In this lesson, we learned:
plt.plot() creates a line chart.plt.title() adds a chart title.plt.xlabel() and plt.ylabel() add axis labels.marker displays individual data points.color, linewidth, and linestyle customize the line.plt.grid() displays gridlines.plt.legend() identifies different lines.plt.annotate() displays values near data points.plt.savefig() saves the chart as an image.| Term | Meaning |
|---|---|
| Line Chart | A chart that connects data points using lines. |
| Matplotlib | A Python library for creating charts. |
| Pyplot | Matplotlib’s plotting interface. |
| X-axis | Horizontal axis. |
| Y-axis | Vertical axis. |
| Data Point | A point representing a numerical observation. |
| Marker | A symbol showing a data point. |
| Gridlines | Reference lines that help read chart values. |
| Legend | Identifies different datasets. |
| Trend | General direction of data changes. |
| Fluctuation | Repeated increases and decreases. |
| Annotation | Additional information displayed near a chart element. |
| Multiple Line Chart | A chart containing two or more lines. |
For instructors and self-assessment. Students should attempt all questions before checking these answers.
Assessment answer key
13 questions
Reveal answer key
By completing this lesson and its exercises, students should be able to:
Lesson completed
Teaching recommendation: Teach Sections 1–7 before starting Python coding. Demonstrate Sections 8–9 live in class, discuss Sections 10–12 using actual charts, and assign Sections 13–15 as guided and independent practice.
This keeps the learning progression consistent: Understand → Prepare → Create → Analyze → Practice.