How to Make a Symmetric Logarithmic Axis in PGFPlots
At a glance
- 1. Choose the mapping
Set a linear region around zero and a logarithmic region outside it.
- 2. Transform x values
Map each original x coordinate to its position on the symlog axis.
- 3. Label original values
Place ticks at transformed positions and label them with the untransformed values.
- 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 , use the piecewise transform
Values from to 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, map to .
For a different positive linear threshold , a corresponding mapping is
This keeps the linear region continuous with the logarithmic part at . Other symlog conventions exist, so state the threshold and formula when they matter.
Worked PGFPlots example
The code plots at sample values from to . 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 is plotted at transformed position , 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 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 go at positions . 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
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.