1

I stumbled over a perfect example for a diagram in this forum, which I can use (see: Symlog as axis scaling in PGFPlots). It works very fine except the error bars. Here is my example:

\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}

enter image description here

There could be some experimental data in "data.dat" and the corresponding error. In this example this error is 1.0. As you can see this error is fine for the linear part of the diagram, but not for the logarithmic part. It becomes clear, that the error bar is not transformed correctly due to the symlog function. I searched for a solution in the pgfplot documentary but didn't find some. Have you got any suggestion?

hooky
  • 13
  • 2

1 Answers1

1

Something like this?

\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}

enter image description here

(I added some data points to check that the error bars are as they should.)