# Mixed pgfplots, setting domain by calculated xmin

I am very new to pgfplots and have a (hopefully) short question. I wanna create a graph with two plots. First one should be created from a text-file (have changed it here to coordinates to make it a little easier) and the second plot should be a function.

The MWE looks like that:

\documentclass{scrartcl}
\usepackage{pgfplots}
\usepgfplotslibrary{units}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\begin{axis}[width=0.7\linewidth]
%Masse
coordinates {
(0, 0)
(1, 1)
(2, 2)
(3, 3)
(10, 10)
};
%lineare Regression
{1*x};
\end{axis}
\end{tikzpicture}

\end{document}


It's working fine but now I want to change the the function's domain, depending from the calculated values of xmin and xmax of first plot. I thought about something like:

\addplot[red,domain=\pgfplots@data@xmin : \pgfplots@data@xmax]


or

\addplot[red,domain=\pgfkeysvalueof{/pgfplots/xmin} : \pgfkeysvalueof{/pgfplots/xmax}]


But it's not working and I hope for some help from you.

Thnaks and best regards

• Noone have an idea?!? Even with searching the internet the last days, I haven't found a solution. But I cannot imagine, that nobody else had this problem before... Maybe I'm just too studip to find the right answer. Aug 14 '16 at 19:01

Here is a way, though I have to say it's not super elegant. It uses pgfplotstable. I start by saving the data to a table. Assuming the data is sorted by x-value, you can extract the smallest x-value with \pgfplotstablegetelem{0}{x}\of\mydata, where x is the column name, and \mydata is the table. I don't know of a way to get the last value in a column though, so I make a new table where the x-values are sorted descending, and get the first x-value of this new table as the maximum.

That said, pgfplots can calculate linear regressions for you, which might be of interest, see second tikzpicture in code.

\documentclass{scrartcl}
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{units}

x y
0 0
1 1.2
2 1.8
3 3.2
10 9.5
}\mydata
% extract first x-value and save to \TableMin
\pgfplotstablegetelem{0}{x}\of\mydata
\pgfmathsetmacro{\TableMin}{\pgfplotsretval}

% flip table
\pgfplotstablesort[sort cmp={float >}]\mydatainv\mydata
% extract first x-value from flipped table, save to \TableMax
\pgfplotstablegetelem{0}{x}\of\mydatainv
\pgfmathsetmacro{\TableMax}{\pgfplotsretval}
\begin{document}

\begin{tikzpicture}
\begin{axis}[width=0.7\linewidth]
%Masse
table[x=x,y=y] {\mydata};
%lineare Regression
{1*x};
\end{axis}
\end{tikzpicture}

% linear regression by pgfplots:
\begin{tikzpicture}
\begin{axis}[width=0.7\linewidth]