2025, Nov 02 01:00

Fixing Matplotlib Log-Log Plots: Minor Ticks Disappear with Base 2? Use Bases 8 and 4

Learn why Matplotlib log-log plots lose minor ticks with logarithmic base 2 and how to restore a readable grid using bases 8 and 4 for clear tick marks.

Log–log plots are great for compressing orders of magnitude and revealing multiplicative relationships. But aesthetics matter: evenly spaced minor ticks visually signal that both axes are logarithmic. If you switch the logarithmic base in Matplotlib to 2, those minor ticks disappear and the grid looks uniformly spaced. The data are correct, yet the plot becomes harder to read. Here’s why it happens and how to get readable, logarithmic-looking grids back.

Reproducing the setup with the default base

The following snippet builds a log–log plot using Matplotlib’s defaults. The result includes well-known minor ticks that make the log scaling obvious.

import numpy as np
import matplotlib.pyplot as plt
xvals = np.array([8, 16, 32, 64, 128, 256, 512, 1024])
yvals = np.array([2.412, 3.424, 5.074, 7.308, 11.444, 18.394, 30.644, 48.908])
log_base = 2  # base placeholder
fig_obj, ax_obj = plt.subplots(figsize=(8, 6))
ax_obj.plot(xvals, yvals, 'o--')
ax_obj.set_xscale('log')
ax_obj.set_yscale('log')
ax_obj.set(title='loglog')
ax_obj.grid()
ax_obj.grid(which='minor', color='0.9')

With the default base, the minor ticks appear at familiar positions between major ticks, giving a dense yet readable log grid.

Switching to base 2 and losing minor ticks

Now switch both axes to base 2. The plotted values are still correct, but the grid no longer shows those characteristic logarithmic minor ticks, and spacing looks uniform.

import numpy as np
import matplotlib.pyplot as plt
xvals = np.array([8, 16, 32, 64, 128, 256, 512, 1024])
yvals = np.array([2.412, 3.424, 5.074, 7.308, 11.444, 18.394, 30.644, 48.908])
log_base = 2
fig_obj, ax_obj = plt.subplots(figsize=(8, 6))
ax_obj.plot(xvals, yvals, 'o--')
ax_obj.set_xscale('log', base=log_base)
ax_obj.set_yscale('log', base=log_base)
ax_obj.set(title='loglog')
ax_obj.grid()
ax_obj.grid(which='minor', color='0.9')

Alternative attempts such as using ax.loglog(..., base=...) or formatting tick labels manually don’t restore the evenly spaced minor ticks.

Why this happens

With base 10, you get major ticks at 10, 100, 1000, ... and minor ticks at 20, 30, 40, .... . With base 2 you get major ticks at 2, 4, 8, 16, ... . There aren’t logical positions to place multiple minor ticks between the major ticks.

That’s the crux. When the base is 10, there’s a natural set of minor subdivisions within each decade: 2×, 3×, …, 9×. For base 2, major ticks already land on powers of two, leaving no obvious multi-step minor structure in between. As a result, Matplotlib can’t place a series of meaningful minor ticks that look “logarithmically spaced.”

The practical fix: pick bases that provide meaningful subdivisions

If aesthetics and readability are the goal, choose logarithmic bases that create useful subdivisions. Using base 8 on the x-axis and base 4 on the y-axis yields a grid that looks and reads more like the default base 10 plot, while keeping the scaling aligned with your data ranges.

import numpy as np
import matplotlib.pyplot as plt
xvals = np.array([8, 16, 32, 64, 128, 256, 512, 1024])
yvals = np.array([2.412, 3.424, 5.074, 7.308, 11.444, 18.394, 30.644, 48.908])
x_base_val = 8
y_base_val = 4
fig_obj, ax_obj = plt.subplots(figsize=(8, 6))
ax_obj.plot(xvals, yvals, 'o--')
ax_obj.set_xscale('log', base=x_base_val)
ax_obj.set_yscale('log', base=y_base_val)
ax_obj.set(title='loglog')
ax_obj.grid()
ax_obj.grid(which='minor', color='0.9')

This preserves the correct log scaling while restoring a visually helpful grid, thanks to bases that allow more intuitive placements for tick marks.

Why this detail matters

In exploratory analysis and in reports, perception guides interpretation. When minor ticks vanish or look evenly spaced, readers may miss that they’re looking at a log–log chart. Sensible base choices make the structure of the scale self-evident and reduce cognitive load when comparing slopes or relative growth.

Takeaways

Log–log plots are only as readable as their tick structure. If switching to base 2 eliminates the familiar minor ticks, it’s not a rendering bug but a consequence of the base itself. Pick bases that expose meaningful subdivisions on each axis—in this case, base 8 for x and base 4 for y—to retain a grid that communicates the logarithmic nature of the plot at a glance.

The article is based on a question from StackOverflow by Igor Soares and an answer by Igor Soares.