Line charts

Matplotlib Basics

2 min read

Published Oct 5 2025


15
0
0
0

ChartsGraphsMatplotlibNumPyPandasPythonVisualisation

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-values
  • y = sequence of y-values
  • fmt (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 plt

x = [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()

matplotlib line basic example




Line format shorthand

Optional shorthand way of formatting the line and markers.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.plot(x, y, 'r--o')
plt.show()

matplotlib line format example

Shorthand options:

Symbol

Meaning

Example

-

solid line

'b-'

--

dashed line

'g--'

-.

dash-dot line

'r-.'

:

dotted line

'k:'

o

circle marker

'bo'

s

square marker

'rs'

^

triangle marker

'g^'





Colour and style customisation

Alternative way of styling the line and markers, with parameters for each format choice.

import matplotlib.pyplot as plt

x = [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()

matplotlib line long format example

Styling parameters:

Parameter

Description

Example

color

Line colour

'red', '#00FF00', (0.1, 0.2, 0.5)

linestyle

Style of the line

'-', '--', '-.', ':'

linewidth

Thickness of the line

linewidth=2

marker

Symbol for data points

'o', 's', '^', '*'

markersize

Size of the markers

markersize=10

markerfacecolor, markeredgecolor

Marker fill & border colours

'red', '#00FF00', (0.1, 0.2, 0.5)





Multiple lines on the same chart

import matplotlib.pyplot as plt

x = [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()

matplotlib line multiple lines example




Line with markers only (no connecting line)

import matplotlib.pyplot as plt

x = [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()

matplotlib line just markers example




Fill area under a line

import matplotlib.pyplot as plt

x = [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()

matplotlib line filled area example
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact