4

I am trying to plot data an analyze how well it fits a predicted behaviour. I use pgfplots to plot the data using the command \addplot[color=black,mark=x,only marks] table {data.dat}; I would like to plot a function on top of the data using the command \addplot[color=black,mark=none] {x*x};:

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{blindtext}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}
  \begin{tikzpicture}

    \begin{axis}

    \addplot table {
    1 1
    2 4.3
    3 8.7
    4 15.3
    };

    \addplot[color=black,mark=none] {x*x};

    \end{axis}
  \end{tikzpicture}

  \blindtext

\end{document}

Unfortunately, the domain of the function does not necessarily match the domain of the data. I could specify the domain of the function explicitly to match the min/max of my data, but I am importing the data from somewhere else and those values might change occasionally. Is there a way to adjust the function domain automatically?

3

PGFPlots stores the limits of the data it has encountered so far in macros called \pgfplots@xmin, \pgfplots@xmax, and so on. You can use these to specify the range of your function domain:

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\makeatletter
\newcommand{\pgfplotsxmin}{\pgfplots@xmin}
\newcommand{\pgfplotsxmax}{\pgfplots@xmax}
\makeatother

\begin{document}
  \begin{tikzpicture}

    \begin{axis}

    \addplot table {
    1 1
    2 4.3
    3 8.7
    4 15.3
    };

    \addplot [
            color=black,
            mark=none,
        domain=\pgfplotsxmin:\pgfplotsxmax] {x*x};

    \end{axis}
  \end{tikzpicture}


\end{document}
  • Much cleaner than mine... :) I looked for something like this and everything I read said they weren't computed until \end{axis}. – Paul Gessler Mar 2 '14 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.