plplot.org

Plate 03 · Log-log plot

Log-log plots: what a straight line means, and why it does not prove a power law

By plplot.org ·

A log-log plot uses a logarithmic scale on both axes. Equal distances along an axis stand for equal ratios, not equal differences: the step from 1 to 10 is as long as the step from 100 to 1000. That makes it the plot of choice for data that span several orders of magnitude, and for relationships of the form y = a·x^k, which become straight lines with slope k.

When to use log axes

Claus Wilke, in Fundamentals of Data Visualization, recommends a log scale for quantities produced by multiplication or division, for ratios, and for data whose values differ by orders of magnitude, such as county populations that range from 82 to over four million. On a linear axis the large values squash everything else into a corner.

Use log-log when both variables behave this way, for example frequency and power in a spectrum. If only y spans orders of magnitude, a log-linear plot (log y, linear x) is the right choice; exponential growth is a straight line there (Wilke, chapter 14).

Labelling the axes

Label the axis with the name of the variable and let the tick labels show the values (1, 10, 100 or 10⁰, 10¹, 10²). Wilke points out that “log(x)” as an axis title is ambiguous, because it does not say which base. If you plot log-transformed numbers on a linear axis instead, state the base.

Zero cannot appear on a log axis, because the logarithm of zero is minus infinity. Data that contain zeros need a different treatment, not a quiet removal of those points.

A straight line is not proof of a power law

The most common misuse of log-log plots is to draw a histogram on log-log axes, fit a straight line by least squares and report its slope as the exponent of a power law. Clauset, Shalizi and Newman (2009) showed that this gives biased answers. In their test on synthetic data with a true exponent of 2.5, least-squares fits to the histogram returned values around 1.2 to 1.5, while maximum-likelihood estimation recovered 2.5. The regression also gave no warning: its error estimates did not reveal the bias, and a high r² proved nothing.

They also show that small samples from a power law, a log-normal and an exponential distribution can all look roughly straight on a log-log plot. A straight line is necessary but not sufficient. Their recommended procedure is to estimate the exponent by maximum likelihood, test the fit with a goodness-of-fit test, and compare the power law against alternatives such as the log-normal with likelihood-ratio tests. To show the data, they plot the complementary cumulative distribution, the fraction of values at least x, which is less noisy in the tail than a histogram.

Making it

Most libraries switch an axis to log scale with one setting. In matplotlib, loglog does both axes at once:

import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

x = 10 ** np.arange(0, 4.01, 0.25)
y = 2 * x**1.5

fig, ax = plt.subplots(figsize=(4, 3))
ax.loglog(x, y, "o-")
ax.grid(True, which="both", linewidth=0.3)
ax.set_xlabel("Frequency (Hz)")
ax.set_ylabel("Power")
fig.tight_layout()
fig.savefig("loglog-matplotlib.png", dpi=150)

PLplot works differently: you plot the base-10 logarithm of the data and ask plenv for logarithmic axis labels, so the axes read 10⁰ to 10⁴ while the numbers drawn are 0 to 4. The figures below, and their code in Plot Rosetta, show both approaches.

Sources

  1. Fundamentals of Data Visualization, chapter 3: Axes and coordinate systemsClaus O. Wilke
  2. Fundamentals of Data Visualization, chapter 14: Visualizing trendsClaus O. Wilke
  3. Power-law distributions in empirical dataClauset, Shalizi and Newman, SIAM Review 51(4), 2009