# Function with the same domain as data loaded into pgfplots

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}

1 1
2 4.3
3 8.7
4 15.3
};

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

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}

1 1
2 4.3
3 8.7
4 15.3
};


• 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