Let's put it together: a complete, labelled chart from data to display. This is the skeleton you'll reuse for almost every plot — create, draw, label, show.
The full recipe
import matplotlib.pyplot as pltimport numpy as npx = np.linspace(0, 2 * np.pi, 200)fig, ax = plt.subplots(figsize=(8, 4))ax.plot(x, np.sin(x), label="sin")ax.plot(x, np.cos(x), label="cos") # a second lineax.set_title("Trig functions")ax.set_xlabel("radians")ax.set_ylabel("value")ax.legend() # show the labelsax.grid(True)plt.show()
The reusable pattern
- Create:
fig, ax = plt.subplots() - Draw:
ax.plot(...)(once per series, with alabel=) - Label: title, axis labels,
ax.legend() - Show or save:
plt.show()
Key takeaways
- 01The core flow: create (subplots) → draw (ax.plot) → label → show.
- 02Overlay multiple series by calling ax.plot several times.
- 03Give each series a label= and call ax.legend() to identify them.
- 04figsize=(w, h) sets the figure size in inches.
