[Tex/LaTex] Making a log plot with negative values “continuous”

pgfplotsplot

I'm trying to plot some data on a log axis using PGFplots. The data include negative values. Obviously, these can't be displayed on the plot, but I'd like the plot to "run into" the lower edge to indicate that it continues to lower values. How can I do this?

Here's an example using the function x^2 - 1 (although the actual data I'm working with is read from files and stored in a macro, I'm not plotting a formula). If I plot the function on a log axis,

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
 \begin{tikzpicture}
  \begin{axis}[ymode=log,domain=-5:5,unbounded coords=jump]
   \addplot {x^2 - 1};
  \end{axis}
 \end{tikzpicture}
\end{document}

it looks like this:

current behavior

I would like to get it to look like this:

desired behavior

In this case I managed to fake it by adding in an extra point below the range of the graph, but that doesn't help me when I'm reading in data points from a file. I can write a script to preprocess my file to add in extra data points, if that's what it takes, but I'd prefer to take care of it by tweaking a setting in PGFplots, or doing something else that's not too complicated in pure (La)TeX if possible.

Best Answer

You can use a coordinate filter to replace the negative values with very small values, and use ymin to set the visible range to a value that's larger than the small replacement value:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
 \begin{tikzpicture}
  \begin{axis}[
    ymode=log,
    log basis y=10,
    domain=-5:5,
    ymin=0.1,
    y filter/.code={% If the y value is undefined (because it was the log of a negative number)
    % replace it with a value that's smaller than log(ymin)
        \ifx\pgfmathresult\empty\def\pgfmathresult{-2}\fi%
    }]
   \addplot {x^2 - 1};
  \end{axis}
 \end{tikzpicture}
\end{document}