[Tex/LaTex] “Logarithmic scale” for negative values

pgfplotspgfplotstabletikz-pgf

I have the following MWE:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\pgfplotsset{compat=1.12}

\begin{filecontents}{data.dat}
4 -1e4
4 -1e3
4 -1e2
4 -10
4 -1
\end{filecontents}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            xmin=0, xmax=5
        ]
        \addplot table {data.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

This produces the following plot:

A normally scaled plot

Now I'd like to have ticks on the y axis at -1e0, -1e1, -1e2, -1e3 and -1e4 with even spacing. If my values are positive, I can achieve this by adding ymode=log to the axis options, but for negative values this just produces an error.

So how can I produce a sort of logarithmic scale for negative values?

Best Answer

This works, but requires copious manual intervention. Note, in this case [ytick=data] would achieve the same effect, but in general you can't count on that.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\pgfplotsset{compat=1.12}

\begin{filecontents}{data.dat}
x y
4 -1e4
4 -1e3
4 -1e2
4 -10
4 -1
\end{filecontents}

\begin{document}
% create column "log"
\pgfplotstableread{data.dat}\table
\pgfplotstablecreatecol[create col/expr={-log10(-\thisrow{y})}]{log}\table
%\pgfplotstabletypeset\table
\begin{tikzpicture}
    \begin{axis}[ytick={-4,-3,-2,-1,-0},yticklabels={-1e4,-1e3,-1e2,-10,-1},
            xmin=0, xmax=5
        ]
        \addplot table[x=x, y=log] \table;
    \end{axis}
\end{tikzpicture}
\end{document}

demo