Tx · LaTeX & TikZ

How to Make a Symmetric Logarithmic Axis in PGFPlots

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the mapping

    Set a linear region around zero and a logarithmic region outside it.

  2. 2. Transform x values

    Map each original x coordinate to its position on the symlog axis.

  3. 3. Label original values

    Place ticks at transformed positions and label them with the untransformed values.

  4. 4. Plot and check

    Keep y values unchanged, then verify the zero and threshold ticks.

A symmetric logarithmic (symlog) axis shows negative and positive values on one axis, with a linear region near zero and logarithmic spacing farther away. It is useful when data cross zero but also span several orders of magnitude.

PGFPlots does not have a standard xmode=symlog setting. The reliable workaround is to transform the plotted x coordinates yourself and manually set the tick positions and labels. The example below uses a linear threshold of 1 and base-10 logarithmic spacing beyond it.

Define the symlog mapping

For threshold L=1L=1, use the piecewise transform

f(x)={x,∣x∣≤1,sgn⁡(x)(1+log⁡10∣x∣),∣x∣>1.f(x)=\begin{cases} x, & |x|\leq 1,\\ \operatorname{sgn}(x)\bigl(1+\log_{10}|x|\bigr), & |x|>1. \end{cases}

Values from −1-1 to 11 retain their linear spacing. Outside that interval, each factor of ten adds one unit of distance from the origin. The mapping is symmetric, and zero remains at zero. For example, −100,−10,−1,0,1,10,100-100,-10,-1,0,1,10,100 map to −3,−2,−1,0,1,2,3-3,-2,-1,0,1,2,3.

For a different positive linear threshold LL, a corresponding mapping is

f(x)={x/L,∣x∣≤L,sgn⁡(x)(1+log⁡10(∣x∣/L)),∣x∣>L.f(x)=\begin{cases} x/L, & |x|\leq L,\\ \operatorname{sgn}(x)\bigl(1+\log_{10}(|x|/L)\bigr), & |x|>L. \end{cases}

This keeps the linear region continuous with the logarithmic part at ∣x∣=L|x|=L. Other symlog conventions exist, so state the threshold and formula when they matter.

Worked PGFPlots example

The code plots y=x/(1+∣x∣)y=x/(1+|x|) at sample values from −100-100 to 100100. The x coordinates in coordinates are already transformed; the tick labels show the original values.

\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xlabel={$x$ (symlog; labels show original values)},
  ylabel={$x/(1+|x|)$},
  xmin=-3, xmax=3,
  xtick={-3,-2,-1,0,1,2,3},
  xticklabels={$-100$,$-10$,$-1$,$0$,$1$,$10$,$100$},
  ymin=-1.1, ymax=1.1,
  grid=both,
]
\addplot[blue, thick, mark=*] coordinates {
  (-3,-0.9901)
  (-2,-0.9091)
  (-1,-0.5)
  (0,0)
  (1,0.5)
  (2,0.9091)
  (3,0.9901)
};
\end{axis}
\end{tikzpicture}
\end{document}

For example, the original x value 1010 is plotted at transformed position f(10)=2f(10)=2, so the point appears at (2,0.9091). Its tick is also at 2, but its label is 10. The y coordinates remain the function’s ordinary values; only the x positions are transformed.

Applying the method to your data

For each original x value, calculate f(x)f(x) and use that result as the coordinate supplied to PGFPlots. Keep a mapping table or script so the transformed coordinates can be checked and regenerated if the data change. Then choose ticks in the original units, transform each tick position with the same formula, and use the original values as the tick labels.

For instance, with threshold 1, ticks at −100,−10,−1,0,1,10,100-100,-10,-1,0,1,10,100 go at positions −3,−2,−1,0,1,2,3-3,-2,-1,0,1,2,3. If you use a different threshold or logarithm base, recompute both the plotted positions and tick locations using that same mapping. Do not apply an ordinary log scale as well: the x values are already transformed.

PGFPlots also has coordinate-transformation hooks, such as x coord trafo and its inverse counterpart, that can automate transformations in more elaborate plots. If you use them, make sure the transform, inverse transform, axis limits, and tick handling agree. The explicit-coordinate approach above is easier to inspect and works without depending on custom axis-transform behavior.

Common mistakes

  • Using xmode=log: a logarithmic axis cannot represent zero or negative x values.
  • Transforming coordinates but not ticks: ticks must be placed at transformed positions, even when their labels display original values.
  • Transforming only the tick labels: labels do not move plot data. Transform the plotted x coordinates too.
  • Changing the threshold inconsistently: use the same threshold and base for every data point, tick, and limit.
  • Forgetting the zero neighborhood: symlog is linear near zero; do not replace zero with an arbitrary small positive value.

If the plot is generated from a function rather than a finite data set, sample it in original x, transform those sample positions, and plot the resulting coordinates. This represents the intended function with symlog spacing on the horizontal axis; as with any finite sampling, line segments approximate the curve between sample points.

References

  1. PGFPlots Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.