Line charts
Matplotlib Basics
2 min read
Published Oct 5 2025, updated Aug 17 2026
Guide Sections
Guide Comments
A line chart connects data points with straight lines, typically used to show trends over time or continuous relationships between variables.
Syntax:
plt.plot(x, y, fmt, **kwargs)Parameters:
x= sequence of x-valuesy= sequence of y-valuesfmt(optional) = a shorthand string that defines:- line style (
-,--,-.,:) - marker (
o,s,^, etc.) - colour (
r,g,b, etc.)
Example:plt.plot(x, y, 'ro--') means:
r = red, o = circle marker, -- = dashed line
Basic example
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]plt.plot(x, y)plt.title("Basic Line Chart")plt.xlabel("X Axis")plt.ylabel("Y Axis")plt.show()
Line format shorthand
Optional shorthand way of formatting the line and markers.
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]plt.plot(x, y, 'r--o')plt.show()
Shorthand options:
Symbol | Meaning | Example |
| solid line |
|
| dashed line |
|
| dash-dot line |
|
| dotted line |
|
| circle marker |
|
| square marker |
|
| triangle marker |
|
Colour and style customisation
Alternative way of styling the line and markers, with parameters for each format choice.
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]plt.plot(x, y, color='magenta', linestyle='-', linewidth=2, marker='^', markersize=8)plt.title("Customised Line Chart")plt.show()
Styling parameters:
Parameter | Description | Example |
| Line colour |
|
| Style of the line |
|
| Thickness of the line |
|
| Symbol for data points |
|
| Size of the markers |
|
| Marker fill & border colours |
|
Multiple lines on the same chart
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]y1 = [1, 4, 9, 16, 25]y2 = [1, 3, 6, 10, 15]plt.plot(x, y1, label='Squares')plt.plot(x, y2, label='Triangular Numbers')plt.legend()plt.title("Multiple Line Series")plt.show()
Line with markers only (no connecting line)
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]plt.plot(x, y, marker='o', linestyle='None', color='orange')plt.title("Markers Only")plt.show()
Fill area under a line
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]plt.plot(x, y, color='blue')plt.fill_between(x, y, color='lightblue', alpha=0.4)plt.title("Filled Area Under Line")plt.show()