Saturday , December 5 2026
Line Plot using Matplotlib

Line Plot using Matplotlib

Updated: 28 November 2026Level: BeginnerReading time: about 10 minutesMatplotlib 3.x

A line plot connects data points with lines and is the best chart for showing how a value changes over time or along an ordered axis. In Matplotlib you draw it with plt.plot().

What You Will Learn

  • Draw a basic line plot
  • Add a title and axis labels
  • Style text, line colour, width and style
  • Add markers
  • Plot several lines with a legend
  • Grids, tight_layout and xkcd style

Prerequisites

Introduction to Matplotlib.

Basic Line Plot

Example
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
plt.plot(X, Y)
plt.show()
Basic line plot
Basic line plot

Explanation: plot(X, Y) draws straight lines between the points (2, 36), (4, 19) and so on.

Only Y Values

Example
plt.plot([36, 19, 30, 52, 25])
plt.show()
Line plot with automatic x values
Line plot with automatic x values

Explanation: With one list, Matplotlib treats it as Y values and uses 0, 1, 2, 3, 4 as X – Python counts from 0.

How Matplotlib generates x values
How Matplotlib generates x values

Title and Labels

Example
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
plt.title("Info")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.plot(X, Y)
plt.show()
Line plot with title and axis labels
Line plot with title and axis labels

Font Style and Colour

Example
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
style = dict(fontsize=20, fontname="Times New Roman", color="blue", weight="bold")
plt.title("Info", **style)
plt.xlabel("X axis", **style)
plt.ylabel("Y axis", **style)
plt.plot(X, Y)
plt.show()
Styled title and labels
Styled title and labels

Explanation: Putting the shared settings in a dictionary and unpacking it with **style avoids repeating them three times.

Code Colour
b blue
g green
r red
c cyan
m magenta
y yellow
k black
w white
Matplotlib colour codes
Matplotlib colour codes

Any CSS name (“orange”) or hex code (“#0f766e”) also works.

Line Width and Style

Example
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
plt.plot(X, Y, color="k", linewidth=3, linestyle="dashed")
plt.show()
Dashed black line
Dashed black line
Thicker line
Thicker line
Matplotlib line styles
Matplotlib line styles
linestyle Looks like
“solid” or “-“ continuous
“dashed” or “–“ dashes
“dotted” or “:” dots
“dashdot” or “-.” dash-dot

Markers

Example
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
plt.plot(X, Y, color="k", linewidth=3, marker="o", markerfacecolor="red", markersize=10)
plt.show()
Line with red circle markers
Line with red circle markers

Explanation: Markers show the actual data points. Common markers: “o” circle, “s” square, “^” triangle, “*” star, “x” cross.

Several Lines with a Legend

Example
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
plt.plot(X, [36, 19, 30, 52, 25], color="k", linewidth=3, marker="o", label="Chandigarh")
plt.plot(X, [6, 49, 10, 72, 45], color="blue", linewidth=3, marker="o", label="Delhi")
plt.legend()
plt.show()
Two lines on one chart
Two lines on one chart
Two lines with a legend
Two lines with a legend

Explanation: Give each line a label and call plt.legend() to explain which line is which.

Real Example: Salary by Age

Example
import matplotlib.pyplot as plt
ages = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
salary = [38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(ages, salary, linewidth=3, label="Python developers")
plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Salary (USD) by Age")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
Median salary by age
Median salary by age

Explanation: grid(True) adds guide lines; tight_layout() adjusts spacing so labels are not cut off.

Fun: xkcd Style

Example
with plt.xkcd():
    plt.plot(ages, salary, linewidth=3)
    plt.title("Median Salary by Age")
    plt.show()
Line plot in xkcd hand-drawn style
Line plot in xkcd hand-drawn style

Explanation: Using with plt.xkcd(): applies the hand-drawn style only inside the block; calling plt.xkcd() alone changes every later plot too.

Common Errors

Problem Cause and fix
Font warning for Times New Roman The font is not installed (common on Linux); use “serif” or another installed font.
Lines appear in the wrong order Sort the data by X before plotting.
Nothing is shown in a script Call plt.show().

Series

Matplotlib introduction · Bar plot · Scatter plot · Histogram

Interview Questions

Q: When should you use a line plot?
For ordered or time-based data where the trend matters.

Q: How do you show two series on one chart?
Call plot() twice with labels, then legend().

Practice Exercises

  1. Plot your monthly expenses for 6 months with markers and a title.
  2. Plot two cities’ temperatures over a week with a legend.

Summary

plt.plot draws line charts; add titles, labels, colours, widths, styles, markers, several lines with legends, grids and tight_layout for clear, readable plots.

Next: Bar Plot

About Machine Learning

Check Also

Exploratory Data Analysis (EDA) in Python: Step-by-Step Checklist

A step-by-step EDA checklist in pandas with tested outputs: shape, missing values, describe, value counts, groupby, correlations and charts.