5.4.16 · D1Scientific Computing (Python)

Foundations — 2D plots — line, scatter, bar, histogram, contour, imshow

2,600 words12 min readBack to topic

This page assumes you have seen nothing. We build every symbol the parent note (2D plots — line, scatter, bar, histogram, contour, imshow (index 5.4.16)) leaned on, in an order where each idea rests only on the ones before it.


0. The flat rectangle and its two numbers

Everything starts with a coordinate plane: a flat sheet where any point is pinned down by two numbers.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

Look at the figure. The two crossing lines are the axes (singular: axis):

  • the horizontal one is the x-axis (left–right),
  • the vertical one is the y-axis (down–up).

The point where they cross, , is the origin. Every dot you will ever plot is just "walk steps right (left if negative), then steps up (down if negative) from the origin."


1. A list of numbers: the array

You never plot one point. You plot many, so you need a way to hold many numbers in a row.


2. Evenly spaced numbers: linspace

The parent note wrote np.linspace(0, 2*np.pi, 200). What does that mean?


3. The function machine

Two symbols in the Gaussian example need spelling out first:

The symbols the parent used and what they mean as machines:

Symbol Plain words Picture
height of a point going round a circle a wave up and down between and
half a full turn around a circle
a full turn one complete wiggle of
a "bump" that is tallest at the middle a smooth hill fading to outwards

(about ) is just a special fixed number. Since , the bump equals at the origin (where ) and shrinks fast as the point moves outward (as grows) — that is the Gaussian bump used for the contour example.


4. From a line of numbers to a grid: two indices

A line plot needs a 1D list. A contour or an image needs a 2D table — numbers arranged in rows and columns. That demands two addresses instead of one.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

Study the figure: it is a spreadsheet. The address (row 1, column 2) picks out one cell. This is exactly what imshow colors — one cell = one colored block — and what contour reads heights from.


5. Turning two 1D axes into a grid: meshgrid

You have xs (a row of x-values) and ys (a row of y-values). You want the height at every combination. meshgrid builds the two lookup tables for you.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

6. Numbers become a length: the bar height

For a line plot a number is a position; but a bar turns a number into a length — a rectangle grows tall in proportion to its value.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

7. Numbers become color: the colormap

For a line plot, a number becomes a position; for a bar, a length. For imshow and filled contours, a number becomes a color. That translation needs a rule.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

8. The two containers: Figure and Axes

Before any of the above lands on screen, it needs somewhere to live.


9. Counting for the histogram

The histogram needs one last idea: counting how many samples fall in a range.


How these foundations feed the topic

The topic 2D plots stands on four legs, each built from plain arrays and functions:

Foundation (built above) Feeds… Used by
ordered pair → array → linspace → function points and curves line, scatter
two indices → meshgrid a 2D grid of heights contour
value → color (colormap) colored cells imshow, filled contour
value → length (bar height) tall rectangles bar
counting # + half-open bins frequencies histogram
Figure + Axes a place to draw all of the above every plot

Read it as a chain: ordered pair underlies the array; the array (spaced by linspace) feeds the function; arrays with two indices feed meshgrid; heights get turned into color or length; samples get counted; and the whole lot is drawn inside an Axes sitting on a Figure.


Equipment checklist

Cover the answers; say each aloud before revealing.

What does the ordered pair tell you, including negatives?
Walk right (left if negative) and up (down if negative) from the origin — a single point's position.
In the array , what is and what is the last index?
is how many numbers; the last index is (counting starts at ).
What is the gap between neighbours in linspace(start, stop, N)?
— there are gaps between points.
What does mean and how is it computed for a point ?
The straight-line distance from the origin, , so .
In , which index is the row and which is the column?
= row (vertical, tied to ); = column (horizontal, tied to ).
Why is array indexing "reversed" from ?
It's row-first then column-first, i.e. then — the opposite of our -then- habit.
What do and from meshgrid each depend on?
depends only on the column (the x-value); depends only on the row (the y-value).
How does a bar turn a number into a length?
The rectangle rises from the baseline to height equal to the value; twice the value is twice the height (negative dips below).
What is a colormap, and why is a colorbar needed?
A fixed ramp from numbers to colors; the colorbar labels which color means which number, otherwise color is meaningless.
What is the difference between a Figure and an Axes?
Figure = the whole canvas; Axes = one coordinate box on it (owns an x- and y-axis and your drawing).
What does mean?
The count of samples landing in the half-open range — a histogram bar's height.
Why use a half-open interval for bins?
So a value sitting exactly on a shared edge is counted in only one bin, never twice.