Matplotlib: Your First Charts
Draw line, scatter, bar, and histogram charts with Matplotlib, style them with colors and markers, and compose multi-panel figures with plt.subplots.
Numbers convince analysts; pictures convince everyone else. Matplotlib is Python's foundational plotting library — nearly every other charting tool (including seaborn, next lesson) is built on top of it. Here you'll learn the four workhorse chart types, how to label and style them, and how to arrange several plots in one figure.
The pyplot workflow
The standard import is import matplotlib.pyplot as plt. A minimal plot is
three lines: prepare data, call a plot function, show the figure:
Every plt.* call between the plot function and plt.show() modifies the
current figure — that's how you'll add titles, labels, and more lines. There
is also an object-oriented style (fig, ax = plt.subplots() then ax.plot),
which shines for multi-panel figures — we'll use it in the subplots section.
A fully dressed line chart
A chart without labels is a puzzle. Here's the checklist version — two lines, a title, axis labels, a legend, and a grid:
The compact format strings pack color + marker + linestyle into a few characters:
"r--"— red, dashed line"b-."— blue, dash-dot line"go"— green, circle markers (no line)"k^:"— black, triangle markers, dotted line
Prefer the explicit keywords (color="tab:red", linestyle="--",
marker="o") in real code — they're easier to read six months later.
Scatter plots: relationships between two variables
Use a scatter plot when each point is one observation and you want to see
how two quantities relate. plt.scatter can also encode a third variable
via color and a fourth via size:
The upward trend jumps out immediately — that's the point of a scatter plot.
Bar charts: comparing categories
Bars compare a numeric value across categories. Keep them honest: start the y-axis at zero and sort when order isn't meaningful:
For long category names, plt.barh (horizontal bars) keeps labels readable.
Histograms: the shape of one variable
A histogram chops a numeric variable into bins and counts how many values land in each — it's the fastest way to see a distribution's shape, center, and outliers:
Try changing bins — too few hides structure, too many turns the plot into
noise. Between 20 and 50 is a good starting range for a few thousand points.
Subplots: several charts, one figure
plt.subplots(rows, cols) returns a figure and an array of axes; each axis
is its own little plotting surface with ax.plot, ax.set_title, and friends:
fig.tight_layout() fixes overlapping labels — make it a habit for any
multi-panel figure.
Which chart when?
Line — something changing over a continuous axis (time, x). Scatter — relationship between two numeric variables. Bar — one number per category. Histogram — the distribution of a single numeric variable. When unsure, ask "what question is this chart answering?" first, then pick.
Check your understanding
Q1.Your data is average temperature per month over 3 years. Which chart type fits best?
Q2.What does the format string "r--" mean in plt.plot(x, y, "r--")?
Q3.You call plt.hist(data, bins=3) on 10,000 values and the plot looks like three boring blocks. What should you do?
Q4.fig, axes = plt.subplots(2, 3) — how do you plot on the bottom-right panel?
Exercise: A two-panel sales dashboard
Build a 1×2 figure: on the left, a line chart of 12 months of simulated
sales (an upward trend plus random noise, with circle markers); on the
right, a histogram of 500 simulated order values (normal around 50, no
negatives). Give every panel a title and axis labels, and make the whole
figure figsize=(10, 4).
Matplotlib gives you full control; next lesson, seaborn gives you beautiful statistical charts with a fraction of the code.