Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Logarithmic Axes

DS 2023 | Communicating with Data

Overview

A logarithmic axis is an axis on which equal distances represent equal ratios, not equal differences.

On a normal (linear) axis, the gap between 1 and 2 is the same size as the gap between 1,0011{,}001 and 1,0021{,}002.

On a log axis, the gap between 1 and 10 is the same size as the gap between 10 and 100, and the same size again as the gap between 100 and 1,0001{,}000.

Each step along the axis is a multiplication, usually by 10. These steps are called orders of magnitude.

Use cases

Consider using a log axis when one of these is true.

  • The data span several orders of magnitude. If the largest value is a hundred times the smallest, a linear axis spends almost all of its length on the few big values and crushes everything else against the origin.

  • The distribution is strongly right-skewed. This is the same problem stated in terms of one variable instead of two. Price, income, population, city size, and file size are all like this.

  • The interesting quantity is a ratio. On a log axis, equal percentage differences look equal, so two series that stand in a constant proportion to each other appear as parallel lines, however far apart they are. Applied to a time series, this is how you compare growth rates.

A common example of a logarithmic scale is the Richter scale to measure the magnitude of an earthquake. Each increase in the scale is 10 times the previsous value.

A simple example

To demonstrate the concept, we will use the diamonds data set from Seaborn.

This dataset contains observations of nearly 54,00054{,}000 round diamonds, with their physical measurements, their grades, and their prices.

Loading...

Let’s look at the price variable.

count 53940.0 mean 3933.0 std 3989.0 min 326.0 25% 950.0 50% 2401.0 75% 5324.0 max 18823.0 median 2401.0 Name: price, dtype: float64

We see that price runs from $326 to $18,82318{,}823 — a ratio of nearly 58 to 1 between the cheapest stone and the most expensive.

Note also that the mean 3,9333{,}933 sits well above the median 2,4012{,}401, which is the signature of a right-skewed variable.

To see the shape of that spread, we can sort the from cheapest to most expensive and plot the values in order.

The x axis is just the rank of the diamond; the y axis is its price.

<Figure size 800x400 with 1 Axes>

This is a hockey stick.

The line stays close to the bottom spine for the first thirty thousand diamonds and then climbs steeply at the end.

The y axis is less useful in this plot in that it could be.

Most of its length separates the few most expensive stones, while the cheap half of the data is squashed into a band so thin you cannot tell a $400 diamond from a $4,0004{,}000 one — even though one costs ten times the other.

Now let’s see the same data plotted with logy=True.

<Figure size 800x400 with 1 Axes>

The hockey stick straightens out.

The cheap diamonds are now as legible as the expensive ones, and you can read the price at any point in the ranking instead of only at the top.

Why it matters for scatter plots

The same skew distorts a scatter plot, in the same way that occlusion does.

Here we see price plotted against carat weight, on linear axes.

<Figure size 800x500 with 1 Axes>

The points crowd into the bottom left corner and fan out as they rise.

The relationship is clearly increasing and clearly curved, but most of the data is packed into a small part of the figure, and the upper right is nearly empty.

You can see this clumping clearly in a hexbin.

<Figure size 800x500 with 2 Axes>

With loglog=True we can scale both axes at once.

<Figure size 800x500 with 2 Axes>

Notice how the curve straightens into a band.

We can recover that exponent by fitting a line through the logged values.

exponent: 1.68

An exponent of about 1.68 means that doubling the weight of a diamond multiplies its price by roughly 21.68≈3.22^{1.68} \approx 3.2, not by 2.

So, larger diamonds are disproportionately expensive, which fact is visible as a slope once both axes are logged.

Revealing structure

Note also that the log axis reveals a structure that was invisible before.

The band is striped, with dense vertical lines at round carat weights.

Diamonds cluster at 0.3, 0.5, 0.7, 1.0, 1.5 and 2.0 carats.

This is because cutters sacrifice weight to hit those thresholds.

  • A diamond that weighs exactly 1.00 carat is worth a lot more money than an identical diamond that weighs 0.98 carats, even though the size difference is invisible to the naked eye.

  • Cutters compromise on geometric perfection (cut quality) in order to keep the stone heavy enough to cross that price boundary.

The gap just below 1 carat is the clearest case.

<Figure size 640x480 with 1 Axes>

There are twenty times as many diamonds just above the one-carat line as just below it.

That is not a fact about the instrinsic nature of diamonds.

It has to do with pricing, and the log axis brought it into view.

Parallel lines mean equal ratios

Let’s take the mean price for each combination of carat weight and cut grade, grouped by bins of carat values.

We creat 15 bins, or groups, of data by dividing the scale of value into 15 equally space units.

Loading...
<Figure size 800x400 with 1 Axes>

With linearly scaled x and y axes, the five lines fan apart as they rise.

In this visualization, you might get the impression that cut matters more and more as the diamonds get larger.

But that impression may be deceptive: the lines are separating in dollars, and dollars get larger for everyone.

Now let’s look at the same data with by setting both axes to a log scale with loglog=True.

<Figure size 800x400 with 1 Axes>

In this visualization, the lines appear roughly parallel to each other.

If lines are parallel on a log axis it means a constant ratio separates them.

From this we can infer that the premium for a better cut is not a growing number of dollars.

Instead, it increases as a roughly fixed percentage, applied to a price that is itself rising steeply with weight.

We can read that percentage off the vertical offset between two lines.

Let’s look at the ratio of Ideal to Good prices.

Ratio of Ideal to Good: 1.2

The take-away is that an Ideal-cut diamond costs on the order of 20 percent more than a Good-cut diamond of the same weight, at nearly every weight.

Log axes require ratio data

Everything above assumes something about the level of measurement of the variable being logged.

A log axis makes a claim about ratios: it says that the distance from 10 to 20 means the same thing as the distance from 20 to 40, because both are a doubling.

That claim is only meaningful on a ratio scale — a scale with a true, non-arbitrary zero, where saying “twice as much” refers to something real. Price and carat weight both qualify. A $0 diamond and a 0-carat diamond are not conventions; they are nothing.

On an interval scale, the zero point is a matter of convention. Differences are meaningful but ratios are not, and a log axis will therefore produce a picture that depends on the unit you happened to choose.

Temperature is the standard illustration. Take three temperatures and express them three ways.

Loading...

In Celsius the three values are in the ratios 1:2:41 : 2 : 4.

In Fahrenheit the very same three temperatures are in the ratios 1:1.36:2.081 : 1.36 : 2.08.

In Kelvin they are 1:1.04:1.111 : 1.04 : 1.11.

Since a log axis places points according to those ratios, the three points land at different spacings in each unit.

<Figure size 900x400 with 3 Axes>

In Celsius the points are evenly spaced. In Fahrenheit they are not. Nor are they in Kelvin.

Nothing about the world changed between the three panels — only the arbitrary zero of the scale. Whatever pattern the reader sees is an artifact of the unit.

Three things follow from this.

Some interval scales become ratio scales once converted. Celsius has a conventional zero; Kelvin has a physical one. 200 K really is twice 100 K.

Calendar years are interval; durations are ratio. Logging a year axis is meaningless, since year zero is a convention. Logging age, time since purchase, or years elapsed is fine.

Some variables are already logarithms. Stellar magnitude, decibels, pH, and the Richter scale are logs of underlying ratio quantities, which makes them behave like interval scales: differences are meaningful, ratios are not. Do not log them twice.

Ordinal and nominal variables are out entirely, since their spacings are not quantities at all.

Cautions

Zero and negative values cannot be plotted. The log of zero is undefined and the log of a negative number is not real, so Matplotlib silently drops those points.

The diamonds data has 20 rows where the depth measurement z is recorded as 0 — missing data entered as a zero. They appear on a linear axis and vanish on a log one, with no warning.

20

For a ratio variable that legitimately crosses zero, the solution is a symmetric log scale — ax.set_yscale('symlog') — which is linear through a window around zero and logarithmic outside it.

A log axis flatters exponential growth. A curve that doubles every year becomes a straight line, which reads as steady and unalarming. That is useful for analysis and misleading for persuasion.

It shrinks outliers. A value ten times larger than its neighbors moves only one step along the axis, so extreme points stop looking extreme.

Label the axis. A reader who does not notice the scale will misread every distance in the figure. Tick labels like 103 help, as does saying so in the title or caption.