Customize Line Charts in Python with Matplotlib
Learn how to customize a Matplotlib line chart by changing colors, markers, labels, gridlines, titles, and styling for more effective data visualization.
1. Modify and Customize
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.
2. Complete Code
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()A. Change the chart title
Use plt.title().
plt.title("University Enrollment Report")You can replace the title with any meaningful description.
B. Change line color
Use the color parameter.
plt.plot(months, students, color="red")Other options include:
color="blue"
color="green
"color="orange
"color="purple"C. Change line width
Use linewidth.
plt.plot(months, students, linewidth=3)linewidth=1: Thin line.linewidth=2: Medium line.linewidth=3: Thick line.
D. Change line style
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="-.")E. Add markers
Use marker.
plt.plot(months, students, marker="o")This displays a circular marker at each data point.
F. Change marker size
Use markersize.
plt.plot( months, students, marker="o", markersize=10)Larger values make the markers bigger.
G. Change marker colors
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 |
H. Change figure size
Use plt.figure(figsize=(width, height)).
plt.figure(figsize=(12, 5))
plt.plot(months, students, marker="o")
plt.show()Here:
12represents the figure width in inches.5represents the figure height in inches.
A larger figure provides more space for the chart.
I. Change font size
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()J. Add data labels or annotations
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.
K. Add gridlines
plt.grid(True)You can also customize gridline appearance.
plt.grid( True, linestyle="--", alpha=0.5)Here:
linestyle="--"displays dashed gridlines.alpha=0.5controls transparency.
L. Customize X-axis and Y-axis ticks
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)M. Rotate X-axis labels
Long category names may overlap.
Use:
plt.xticks(rotation=45)For example, month names will be displayed at a 45-degree angle.
N. Add a legend
plt.plot( months, students, label="Enrollment")
plt.legend()We can also change the legend position:
plt.legend(loc="upper left")O. Set axis limits
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.
P. Save the chart as a PNG image
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=300controls image resolution.bbox_inches="tight"reduces unnecessary empty space around the chart.
The image is saved in the current working directory.
A. Multiple Line Chart
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 plt
months = [
"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?
- The blue line represents Computer Science.
- The green line represents Information Technology.
- Both departments show an overall increasing trend.
- Computer Science has higher enrollment throughout the year.
B. Line Chart with Different Styles
We can display multiple line styles to understand how Matplotlib represents different patterns.
import matplotlib.pyplot as plt
months = ["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()C. Line Chart with Markers
Markers make individual data points easier to identify.
import matplotlib.pyplot as plt
days = ["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()D. Horizontal vs Vertical Concept
A standard line chart normally uses:
- A horizontal X-axis.
- A vertical Y-axis.
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 plt
months = [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.