我在这个论坛中偶然发现了一个完美的图表示例,我可以使用它(参见:在 PGFPlots 中,Symlog 作为轴缩放)。除了误差线之外,它工作得很好。这是我的示例:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{math}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
x y error
1 1 1.0
\end{filecontents*}
\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,
ymin = -10,
ymax = 10,
domain = -5:5.5,
ytick = {-10, -1,0,1,10},
minor ytick = {-9,-8,...,-2,-.9,-.8,...,.9,2,3,...,9},
tick label style = {fill=white, fill opacity=.7},
yminorgrids = true,
ymajorgrids = true,
xmajorgrids = true,
samples=200,
axis lines=center,
]
\addplot [only marks, mark=square, error bars/.cd, y dir=both, y explicit] table [x=x, y = y, y error=error]{data.dat};
\addplot+ [mark=none] {x} ;
\end{axis}
\end{tikzpicture}
\end{document}
“data.dat” 中可能有一些实验数据和相应的错误。在此示例中,此错误为 1.0。如您所见,此错误对于图表的线性部分是正常的,但对于对数部分则不然。很明显,由于 symlog 函数,错误栏未正确转换。我在 pgfplot 文档中搜索了解决方案,但没有找到。您有什么建议吗?
答案1
像这样吗?
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{math}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
x y error
1 1 1.0
2 2 0.2
3 2.5 1.3
\end{filecontents*}
\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,
ymin = -10,
ymax = 10,
domain = -5:5.5,
ytick = {-10, -1,0,1,10},
minor ytick = {-9,-8,...,-2,-.9,-.8,...,.9,2,3,...,9},
tick label style = {fill=white, fill opacity=.7},
yminorgrids = true,
ymajorgrids = true,
xmajorgrids = true,
samples=200,
axis lines=center,
]
\addplot+ [mark=none] {x} ;
% from https://tex.stackexchange.com/questions/81693/pgfplots-with-symbolic-x-coords-and-error-bars/136754#136754
\addplot+[forget plot,only marks]
plot[error bars/.cd, y dir=plus, y explicit]
table[x=x,y=y,y error expr={symlog(\thisrow{y}+\thisrow{error},\basis)-symlog(\thisrow{y},\basis)}] {data.dat};
\addplot+[only marks,xticklabels=\empty]
plot[error bars/.cd, y dir=minus, y explicit]
table[x=x,y=y,y error expr={symlog(\thisrow{y},\basis)-symlog(\thisrow{y}-\thisrow{error},\basis)}] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}
(我添加了一些数据点来检查误差线是否正确。)