Line plots are one of the most fundamental and widely used types of visualizations. They are particularly effective for showing trends or the relationship between variables where the order matters, such as data points collected over time or steps in a sequence. A line plot connects individual data points with straight line segments, making it easy to see patterns, increases, decreases, or fluctuations.
In Matplotlib, the primary function for creating line plots is plot()
. This function is available directly through the pyplot
interface (commonly imported as plt
) or as a method on an Axes
object. We'll primarily use the pyplot
interface for simplicity in these initial examples, but remember that these commands operate on the underlying Figure
and Axes
.
To create a line plot, you typically provide two sequences (like lists or NumPy arrays) to the plot()
function: one for the x-coordinates and one for the y-coordinates. Matplotlib draws points at each (x, y)
pair and then connects these points with lines.
Let's visualize a simple quadratic relationship, y=x2.
import matplotlib.pyplot as plt
import numpy as np
# Prepare the data
x_values = np.array([0, 1, 2, 3, 4, 5])
y_values = x_values**2 # Calculate y = x^2
# Create the plot
plt.figure() # Optional: Create a new figure
plt.plot(x_values, y_values)
# Add labels for clarity (more on this in a later section)
plt.xlabel("X Values")
plt.ylabel("Y Values (X squared)")
plt.title("A Simple Line Plot")
# Display the plot
plt.show()
Executing this code will generate a window displaying the plot. You'll see the x-axis ranging from 0 to 5 and the y-axis scaling appropriately to show the squared values. The points (0, 0)
, (1, 1)
, (2, 4)
, (3, 9)
, (4, 16)
, and (5, 25)
are calculated and connected sequentially by blue lines (Matplotlib's default color for the first line).
What happens if you provide only a single list or array to plot()
?
import matplotlib.pyplot as plt
import numpy as np
# Data for y-axis only
data_points = np.array([3, 5, 2, 8, 6])
# Create the plot
plt.figure()
plt.plot(data_points) # Only providing y-values
# Add labels
plt.xlabel("Index")
plt.ylabel("Data Value")
plt.title("Plotting with Implicit X-axis")
# Display the plot
plt.show()
In this case, Matplotlib assumes you want to plot the data_points
against their index. The x-values are implicitly treated as [0, 1, 2, 3, 4]
, corresponding to the position of each value in the input array. This is useful for quickly visualizing a sequence where the x-axis represents discrete steps or simply the order of elements.
You can easily plot multiple lines on the same Axes
by calling the plot()
function multiple times before calling plt.show()
. Matplotlib automatically cycles through default colors for each new line, making them distinguishable.
import matplotlib.pyplot as plt
import numpy as np
# Prepare data for two lines
x = np.linspace(0, 10, 100) # 100 points from 0 to 10
y1 = np.sin(x)
y2 = np.cos(x)
# Create the plot
plt.figure()
plt.plot(x, y1) # First line (e.g., sine wave)
plt.plot(x, y2) # Second line (e.g., cosine wave)
# Add labels and title
plt.xlabel("X Value")
plt.ylabel("Function Value")
plt.title("Plotting Multiple Lines (Sine and Cosine)")
# Display the plot
plt.show()
This code generates a plot showing both the sine and cosine functions over the range 0 to 10. The first call to plt.plot(x, y1)
draws the sine wave (likely in blue), and the second call plt.plot(x, y2)
adds the cosine wave (likely in orange) to the same Axes
. Notice how both plots share the same x and y axes. We will cover adding legends in a later section to clearly identify which line corresponds to which dataset.
Line plots form the basis for many types of visualizations. They are excellent for showing change over time (time series), visualizing mathematical functions, or comparing trends between different groups or experiments. As we proceed, you'll learn how to customize their appearance significantly, changing colors, line styles, and markers.
© 2025 ApX Machine Learning