Line and Scatter Plots
Plot continuous data with ax.plot() and discrete points with ax.scatter(), controlling colour, marker, and line style.
Line and Scatter: Core Plot Types
Line plots connect sequential data points with a line and are ideal for visualising trends over time or continuous functions. Scatter plots draw individual points without connecting lines, making them perfect for exploring relationships between two numeric variables. Both are created on a Matplotlib Axes object: ax.plot() for lines and ax.scatter() for scatter points.
ax.plot() for Line Charts
ax.plot(x, y) draws a line connecting data points in order. Pass x-values as the first argument and y-values as the second. Both can be Python lists, NumPy arrays, or Pandas Series. When only one argument is passed, Matplotlib uses the index (0, 1, 2, ...) as the x-axis automatically. The method returns a list of Line2D Artist objects.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot(x, y) # basic line plot
ax.set_title('Sine Wave')
ax.set_xlabel('x')
ax.set_ylabel('sin(x)')
# plt.show()
plt.close()All lessons in this course
- Figure and Axes Architecture
- Line and Scatter Plots
- Bar Charts and Histograms
- Labels, Titles, and Saving Figures