4

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}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[width=0.7\linewidth]
%Masse  
\addplot[smooth,blue,solid,ultra thick]
coordinates {
(0, 0)
(1, 1)
(2, 2)
(3, 3)
(10, 10)
};
%lineare Regression
\addplot[smooth,red,dashdotted,thick] 
{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

1
  • 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.
    – Max Maier
    Aug 14 '16 at 19:01
1

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}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}

\pgfplotstableread{
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  
\addplot+ [ultra thick]
table[x=x,y=y] {\mydata};
%lineare Regression
\addplot[red,dashdotted,thick,domain=\TableMin:\TableMax] 
{1*x};
\end{axis}
\end{tikzpicture}

% linear regression by pgfplots:
\begin{tikzpicture}
\begin{axis}[width=0.7\linewidth]
\addplot table[x=x,y=y] \mydata;
\addplot [red,dashed,thick] table[x=x,y={create col/linear regression={y=y}}] \mydata;
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

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.