Learn how to build a line chart in Python using Matplotlib, from preparing data to plotting values, labeling axes, and visualizing trends step by step.
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 plt
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
]
plt.plot(months, students)
plt.show()
|  |
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()
|  |
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()
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()
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 data
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
]
# Step 2: Create line chart
plt.plot(
months,
students,
color="blue",
linestyle="-",
linewidth=2,
marker="o",
markersize=7,
label="Student Enrollment"
)
# Step 3: Add chart information
plt.title("Monthly Student Enrollment")
plt.xlabel("Month")
plt.ylabel("Number of Students")
# Step 4: Add data labels
for i in range(len(months)):
plt.annotate(
students[i],
(months[i], students[i]),
textcoords="offset points",
xytext=(0, 8),
ha="center"
)
# Step 5: Add gridlines and legend
plt.grid(True)
plt.legend()
# Step 6: Display chart
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
What have we learned?
We have created a line chart with: