1.4.8 · D1Python & Scientific Computing

Foundations — Matplotlib and Seaborn visualization

2,717 words12 min readBack to topic

Before you can read the parent note Matplotlib and Seaborn visualization, you must own every word it throws at you without warning. This page builds each one from nothing — plain words first, then a picture, then why the topic needs it. Read top to bottom; each idea is a brick for the next.


1. The number — a constant you'll keep meeting

Before the coordinate plane, meet one lonely number that shows up in every wave and circle.

  • The picture: a circle whose curved edge is unrolled into a straight strip; the strip is about diameters long — that length ratio is .
  • Why the topic needs it: the parent writes np.linspace(0, 2*np.pi, 100) everywhere. If is a mystery symbol, that line is a mystery. Now it's just "sweep from to about ."

2. The coordinate plane — where "position" comes from

Everything starts with a flat sheet and two number lines.

Figure — Matplotlib and Seaborn visualization

Look at the red dot in the figure. To find where it sits, we read its shadow onto each axis: it is to the right and up, so we call it . The pair of numbers is the position.

  • The picture: two rulers glued at a corner (the origin), a dot floating above them, with the directions marked on each axis.
  • Why the topic needs it: every single thing a plot draws — a line, a bar, a scatter dot — is ultimately one or more points . If you don't own "point = pair of numbers = place on the sheet," nothing else lands.

3. What a "function" and a "curve" are

The parent note writes y = np.sin(x) and expects a smooth wave. Let's earn that.

If we feed the rule many inputs in a row and plot each resulting point , the dots line up into a curve.

Figure — Matplotlib and Seaborn visualization

In the figure, the red curve is . Each point on it obeys the rule: pick any on the bottom, go up to the curve, and the height is . A curve is nothing more than "all the points a rule produces, drawn at once."

  • The picture: a wave, with a couple of sample points marked to show they came from the rule.
  • Why the topic needs it: line plots (ax.plot) draw curves. The parent note plots sine waves and loss curves — both are "many points from a rule, connected."

We don't derive these here; you only need to know what shape each makes so a plotted curve isn't a mystery.


4. An array — the list of numbers that feeds the plot

The parent writes np.linspace(0, 2*np.pi, 100) and np.arange(1, 51). These make arrays.

To plot a curve you need two arrays of equal length: one of values, one of matching values. Matplotlib pairs them up position-by-position: the 1st with the 1st , and so on.

  • The picture: two vertical stacks of numbers, arrows pairing stack-A's row with stack-B's row.
  • Why the topic needs it: ax.plot(x, y) is "take these two arrays, make points, connect them." Arrays are the raw fuel. (Full details live in 1.4.01-NumPy-arrays-and-operations.)

5. A DataFrame — the labelled table Seaborn loves

The parent's Seaborn examples build a pd.DataFrame (there's pd, the Pandas nickname from the top).

  • The picture: a grid; the top row is column names, the cells below are the numbers/labels.
  • Why the topic needs it: Seaborn's superpower is that you say the column name (x='Optimizer') instead of hand-feeding raw arrays. It reads the table itself. Deep dive: 1.4.02-Pandas-DataFrames-and-data-manipulation.

6. The Matplotlib nesting: Figure ⊃ Axes ⊃ Artist

This is the one hierarchy that trips up every beginner. Build it slowly.

Figure — Matplotlib and Seaborn visualization

In the figure the big black rectangle is the Figure; the red-outlined inner boxes are two Axes; the little wave and text inside them are Artists.

  • Why the topic needs it: the parent's "object-oriented pattern" is literally fig, ax = plt.subplots()make a page, hand me one chart on it. Then ax.plot(...) means "draw on that specific chart." Owning this nesting is the whole reason you can build multi-panel figures.

7. The four coordinate systems (why transform=ax.transAxes exists)

The parent quietly uses transform=ax.transAxes to pin text. Matplotlib actually offers four ways to say "where," nested from innermost to outermost.

Think of them as four zoom levels of "where": exact data value → fraction of one chart → fraction of the page → actual pixels.

  • The picture: the same box labelled twice — once with real data numbers, once with -to- fractions — plus the page-level and pixel wrappers around it.
  • Why the topic needs it: to place a caption "always in the top-left corner" regardless of the data range, you use Axes coordinates. transform=ax.transAxes tells Matplotlib "read these numbers as fractions of the chart box, not as data." Without it, would be a data point that might fall off-screen.

8. Colour, legend, and label — the finishing artists

  • Why the topic needs it: an unlabelled plot is a lie — nobody knows what they're looking at. Every good figure in the parent (and in 4.1.01-Exploratory-data-analysis) labels axes and adds a legend.

How it all feeds the topic

The map below is written in Mermaid, a tiny text language for drawing boxes-and-arrows diagrams (you type the words, it draws the shapes). Read each arrow as "you need the left box before the right box makes sense." Start at any box with no arrow coming in (a raw foundation) and follow arrows forward; every path eventually reaches the topic node at the bottom. We use a diagram here — instead of a paragraph — because a dependency map is exactly the kind of thing eyes read faster than words (the whole point of this topic!).

coordinate plane x y

function makes a curve

Figure holds Axes

array of numbers

DataFrame labelled table

line plot

scatter and histogram

Seaborn statistical plots

Axes coordinates

labels legend colour

Matplotlib and Seaborn visualization

Each row of the parent note is one of these bricks in disguise: a loss curve is a function on arrays drawn as a line on an Axes; a violin plot is a DataFrame handed to Seaborn.


Downstream, these foundations power:


Equipment checklist

Cover the right side and answer each aloud. If any stumps you, reread its section above.

What do np, plt, and pd refer to in the code?
np is NumPy (numbers/arrays), plt is Matplotlib's pyplot (drawing), pd is Pandas (labelled tables) — all set by import lines.
What is and roughly its value?
A fixed universal constant, ; it's how many diameters fit around a circle's edge.
Why does mean "one full turn"?
In radians, one whole circle measures out to , so sweeping to covers one complete rotation / wave.
A point means what physical move on the sheet?
From the origin, go 3 steps right (+x) then 2 steps up (+y).
Which directions are negative on the plane?
Left is and down is .
What does a "function" give you for each input?
Exactly one output number .
A curve is really just…
all the points a rule produces, drawn and connected at once.
In what unit does np.sin expect its angle?
Radians (one full circle = ), not degrees.
What shape does make, and why plot loss on a log y-scale?
A fast drop that flattens; log scale stretches the tiny tail so you can still see it decreasing.
What does np.linspace(0, 2*np.pi, 100) produce?
An array of 100 evenly-spaced numbers from 0 to 2π.
Why must the x and y arrays be the same length?
Matplotlib pairs them position-by-position to make points; unequal lengths can't pair up.
What is a DataFrame and why does Seaborn prefer it?
A table with named columns; Seaborn reads columns by name instead of raw arrays.
Distinguish Figure, Axes, and axis.
Figure = the whole page; Axes = one chart on it; axis = a single x- or y-line inside an Axes.
Name the four Matplotlib coordinate systems.
Data, Axes (fraction of a chart), Figure (fraction of the page), and Display (pixels).
What does transform=ax.transAxes do?
Tells Matplotlib to read positions as 0-to-1 fractions of the chart box, not as data values.
What is colour encoding in a scatter plot?
Using each dot's colour to show a third number beyond its x and y.