Line and Marker Plots with plot(x, y)ΒΆ
The plot() function is matplotlibβs most fundamental drawing method. It plots y versus x as connected lines, discrete markers, or both, depending on the format string you provide. Multiple calls to plot() on the same axes overlay additional series.
Why this matters for data science: Line plots are the default choice for visualizing trends over continuous variables β training loss curves, time-series data, function approximations, and model performance metrics across epochs or hyperparameters. The example demonstrates three common format strings: 'x' for marker-only plots (useful for sparse observations), a plain call for smooth lines (ideal for continuous functions), and 'o-' for connected markers (showing both the data points and the interpolated trend). The markeredgewidth parameter controls marker line thickness, while linewidth adjusts the line weight. Understanding these format strings ('o', 'x', '-', '--', 'o-', etc.) lets you quickly switch between visualization styles without changing the underlying data calls.
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
x = np.linspace(0, 10, 100)
y = 4 + 1 * np.sin(2 * x)
x2 = np.linspace(0, 10, 25)
y2 = 4 + 1 * np.sin(2 * x2)
# plot
fig, ax = plt.subplots()
ax.plot(x2, y2 + 2.5, 'x', markeredgewidth=2)
ax.plot(x, y, linewidth=2.0)
ax.plot(x2, y2 - 2.5, 'o-', linewidth=2)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()