![PGFPlots 的 Symlog](https://linux22.com/image/460899/PGFPlots%20%E7%9A%84%20Symlog.png)
我正在尝试绘制范围在 [-1e8,1e8] 之间的数据,并希望使用对称对数图来表示它。我发现这个解决我的问题的方法但似乎只要数据 >abs(1e5),轴就会失效。一个非常简单的 MWE,即使没有负数据,也显示 y 轴最多只能打印 1.64e4,而且在更大的值下似乎不再准确:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\usetikzlibrary{math}
\tikzmath
{
function symlog(\x,\a){
\yLarge = ((\x>\a) - (\x<-\a)) * (ln(max(abs(\x/\a),1)) + 1);
\ySmall = (\x >= -\a) * (\x <= \a) * \x / \a ;
return \yLarge + \ySmall ;
};
function symexp(\y,\a){
\xLarge = ((\y>1) - (\y<-1)) * \a * exp(abs(\y) - 1) ;
\xSmall = (\y>=-1) * (\y<=1) * \a * \y ;
return \xLarge + \xSmall ;
};
}
\begin{document}
\begin{tikzpicture}
\def\basis{50}
\pgfplotsset
{
y coord trafo/.code={\pgfmathparse{symlog(#1,\basis)}\pgfmathresult},
y coord inv trafo/.code={\pgfmathparse{symexp(#1,\basis)}\pgfmathresult},
yticklabel style={/pgf/number format/.cd,int detect,precision=1},
}
\pgfplotstableread{
0 1
1 1e2
2 1e3
3 1e5
4 1e7
}\dataset
\begin{axis}
[
height=.48\textwidth,
scaled ticks = base 10:0,
domain=0:4,
ytick={1e1,1e3,1e5,1e7},
x tick label style={/pgf/number format/.cd,fixed,fixed zerofill,precision=3}
]
\addplot []
table [x index = {0}, y index = {1}]
\dataset;
\end{axis}
\end{tikzpicture}
\end{document}
这是输出
使用与链接的 OC 几乎完全相同的代码,但将域扩展到 -5:15 会导致同样的问题
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{math}
\tikzmath
{
function symlog(\x,\a){
\yLarge = ((\x>\a) - (\x<-\a)) * (ln(max(abs(\x/\a),1)) + 1);
\ySmall = (\x >= -\a) * (\x <= \a) * \x / \a ;
return \yLarge + \ySmall ;
};
function symexp(\y,\a){
\xLarge = ((\y>1) - (\y<-1)) * \a * exp(abs(\y) - 1) ;
\xSmall = (\y>=-1) * (\y<=1) * \a * \y ;
return \xLarge + \xSmall ;
};
}
\begin{document}
\begin{tikzpicture}
\def\basis{1}
\pgfplotsset
{
y coord trafo/.code={\pgfmathparse{symlog(#1,\basis)}\pgfmathresult},
y coord inv trafo/.code={\pgfmathparse{symexp(#1,\basis)}\pgfmathresult},
yticklabel style={/pgf/number format/.cd,int detect,precision=2},
}
\begin{axis}
[
height=12cm,
legend pos=north west,
scaled ticks = base 10:0,
domain = -5:15,
ytick = {-100,-10, -1,0,1,10,100,1e6},
minor ytick = {-90,-80,...,-20,-9,-8,...,-2,-.9,-.8,...,.9,2,3,...,9,20,30,...,90},
tick label style = {fill=white, fill opacity=.7},
yminorgrids = true,
ymajorgrids = true,
xmajorgrids = true,
samples=200,
axis lines=center,
]
\addplot+ [mark=none] {x} ;
\addplot+ [mark=none] {exp(x)} ;
\addplot+ [mark=none] {-exp(-x)} ;
\legend {$x$,$e^x$,$-e^{-x}$}
\end{axis}
\end{tikzpicture}
\end{document}