Logarithmic & Symlog Scales
A logarithmic scale spaces an axis by powers rather than by equal steps, so data spanning many orders of magnitude fits in one chart and exponential growth straightens into a readable line.
Learn Logarithmic & Symlog Scales in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll switch axes to log with set_yscale('log') , use the loglog / semilogx / semilogy shortcuts, handle zeros and negatives with symlog , and tidy log ticks.
When values explode across magnitudes, a linear axis crushes the small numbers into a flat line near zero. Flip the axis with ax.set_yscale('log') and each gridline becomes a power of ten, so 1, 10, 100, and 1000 are evenly spaced and all visible at once.
What you'll see: two panels of the same doubling data — on the left the early values hug the bottom while the last point shoots up, but on the right the log axis turns the whole series into a clean straight line.
Matplotlib offers one-call shortcuts that plot and set the scale together. ax.semilogy() logs the y-axis, ax.semilogx() logs the x-axis, and ax.loglog() logs both. A power law like y = x² becomes a straight line on a loglog plot, with the slope revealing the exponent.
A plain log scale cannot show zero or negative values. symlog (symmetric log) fixes this: it is linear in a small band around zero — set by linthresh — and logarithmic beyond it on both sides. That makes it perfect for data that crosses zero yet still spans large magnitudes.
What you'll see: the cubic curve sweeping from large negative to large positive values through zero — the symlog axis keeps the small middle readable while compressing the enormous tails on both ends.
Replace each ___ to plot growth data on a log y-axis.
❌ My log plot is blank or warns about non-positive data
Log cannot handle 0 or negatives. ✅ Filter them out, or use set_yscale('symlog') which spans zero.
The linear band is the wrong size. ✅ Tune linthresh= to roughly the smallest magnitude you care about.
logit needs values strictly between 0 and 1. ✅ Keep your data inside the open interval (0, 1) — no exact 0 or 1.
Plot linear, quadratic, and exponential growth on a semilog y-axis so their very different scales all fit.
Lesson complete — magnitudes hold no fear!
You switched axes to log, used the semilog and loglog shortcuts, recognized exponential growth as a straight line, and handled zeros and negatives with symlog.
🚀 Up next: More Plot Types — eventplot, broken_barh, and stairs for timelines, Gantt charts, and step outlines.
Practice quiz
Which call switches an existing y-axis to a logarithmic scale?
- ax.logy()
- ax.set_ylog()
- ax.set_yscale('log')
- ax.yscale(log=True)
Answer: ax.set_yscale('log'). ax.set_yscale('log') flips the y-axis to a log scale.
On a log axis, what does a straight line indicate?
- Exponential growth
- Linear growth
- Constant data
- Random noise
Answer: Exponential growth. Equal distances on a log axis are equal ratios, so exponential growth becomes a straight line.
Which shortcut plots and logs BOTH axes in one call?
- ax.semilogx()
- ax.semilogy()
- ax.plot(log=True)
- ax.loglog()
Answer: ax.loglog(). ax.loglog(x, y) plots and sets both axes to log.
What does ax.semilogy(x, y) do?
- Logs both axes
- Plots and logs only the y-axis
- Logs only the x-axis
- Plots with no log scale
Answer: Plots and logs only the y-axis. semilogy logs the y-axis only while plotting in one call.
Why does a plain log scale fail on zero or negative data?
- It needs integers
- It requires sorted data
- The log of zero is undefined and of a negative is complex
- It only works on the x-axis
Answer: The log of zero is undefined and of a negative is complex. log(0) is undefined and log of a negative is complex, so those points drop or error.
Which scale handles data that crosses zero across large magnitudes?
- log
- logit
- linear
- symlog
Answer: symlog. symlog is linear near zero and logarithmic beyond it, so it spans zero and negatives.
What does the linthresh parameter control on a symlog scale?
- The width of the linear band around zero
- The color of the axis
- The number of ticks
- The line width
Answer: The width of the linear band around zero. linthresh sets how wide the linear region around zero is before the log behavior begins.
Which scale stretches values near 0 and near 1 for probabilities?
- symlog
- logit
- log
- linear
Answer: logit. The logit scale stretches regions near 0 and 1, ideal for proportions in (0, 1).
What does which='both' do in ax.grid() on a log axis?
- Logs both axes
- Removes the grid
- Doubles the figure
- Shows major and minor gridlines
Answer: Shows major and minor gridlines. which='both' draws major and minor gridlines, including the faint minor lines within each decade.
What input does the logit scale require?
- Any real numbers
- Only integers
- Values strictly between 0 and 1
- Only negative values
Answer: Values strictly between 0 and 1. logit needs values strictly in the open interval (0, 1) — no exact 0 or 1.