# Pgfplot coordinates in another pgfplot

How can I transform coordinates from the first axis plot to the second axis plot with a different scale?

One way is to repeat all function calls. But if I've got a random function how can I get the same random function in the second plot? I have to change segment length, amplitude according to the scaling but even then I think a second call to rand will give another curve.

So I tried to extract the coordinates. First I've got them with code from Create a coordinate node at each point of a path. (I can't use \thecoordinateindex value in the foreach loop. Why? I have to use the value 10 explicitly.) I managed to print the coordinates for both axis systems with code from Accessing the logic values of a TikZ coordinate. But how can I do this in a loop? How can I export these data to a file to reread them in the second picture or better how can I transfer them directly?

I've used code from Use macro as coordinate in pgfplots plot. I can read these coordinates everywhere but I cannot write to them within the first axis plot so that I can't use them to transfer the information to the second plot.

I don't want to use the spy library because I want to use other ticks labels in the second plot.

\documentclass{standalone}
\usepackage{varwidth}

\usepackage{pgfplots}

\newcounter{coordinateindex}

% First way to extract coordinate values
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]
{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}

% Second way to extract coordinate values
\makeatletter
\newcommand\xcoord[2][center]{{%
\pgfpointanchor{#2}{#1}%
\pgfmathparse{\pgf@x/\pgf@xx}%
\pgfmathprintnumber{\pgfmathresult}%
}}
\newcommand\ycoord[2][center]{{%
\pgfpointanchor{#2}{#1}%
\pgfmathparse{\pgf@y/\pgf@yy}%
\pgfmathprintnumber{\pgfmathresult}%
}}
\makeatother

% Making coordinate definitions global
\makeatletter
\pgfplots@start@plot@with@behavioroptions{#1,/pgfplots/.cd,#2}%
\pgfplots@PREPARE@COORD@STREAM{#4}%
\begingroup
\edef\@tempa{{#3}}%
\ifpgfplots@curplot@threedim
\expandafter\endgroup\expandafter
\pgfplots@coord@stream@foreach@threedim\@tempa
\else
\expandafter\endgroup\expandafter
\pgfplots@coord@stream@foreach\@tempa
\fi
}%
\makeatother

% Global coordinates
\def\PA{1, 50}
\def\PB{3,150}

% Extract coordinates from path
\tikzset{
put coordinates/.style={
initialize counter/.code={
\setcounter{coordinateindex}{0}
},
initialize counter,
decoration={
show path construction,
moveto code={
\stepcounter{coordinateindex}
\coordinate (#1\thecoordinateindex) at (\tikzinputsegmentfirst);
},
lineto code={
\stepcounter{coordinateindex}
\coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
},
curveto code={
\stepcounter{coordinateindex}
\coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
},
closepath code={
\stepcounter{coordinateindex}
\coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
},
},
postaction={decorate}
},
put coordinates/.default=coordinate
}

\begin{document}

\begin{varwidth}{1.4\linewidth}

% First picture
\pgfmathsetseed{1}
\begin{tikzpicture}
\begin{axis}[
x={(2cm,0)},
y={(0,0.02cm)},
clip = false,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=250,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 6,
ylabel style={rotate=-90},
every axis y label/.style=
{at={(ticklabel* cs:1.02)},anchor=south},
ylabel=$y$,
every axis x label/.style=
{at={(ticklabel* cs:1.02)},below left = 8pt},
every tick/.style={thick},
ytick={0,100,...,400},
xtick={0,1,...,5},
yticklabels={0,100,200,300,400},
xlabel=$x$,
xticklabels={0,1,...,6},
minor ytick ={50,150,...,350},
minor xtick ={0.2,0.4,...,5},
tick align=outside]

% grid
\draw [gray] (0,0) grid (5,200);

% random line
% getting coordinates from path via: put coordinates
%  information is stored in coordinate1, 2,.., 10 and
%   in the counter \thecoordinateindex
\draw [put coordinates, thick] decorate [
decoration = {random steps,
segment length = 5mm,
amplitude = 3mm}]
{ (\PA) -- (\PB)};

\node at (\PA) [left = 1mm] {PA};
\node at (\PB) [right= 2mm] {PB};

% printing \thecoordinateindex (=10)
\node at (1,150) {\thecoordinateindex};

\foreach \i in {1,...,10} % Can't use \thecoordinateindex for 10
{
\edef\temp{\noexpand \fill (coordinate\i)
}
%    \show\temp
\temp
}

\node at (-1.2,200) {axis coord 1:};
\node at (-1.2, 180) {\xcoord{coordinate1},\quad \ycoord{coordinate1}};
\node at (-1.2,160) {axis coord 2:};
\node at (-1.2,120) {axis coord 3:};
\end{axis}

\node at (1,-1) {tikz coord 1: \xcoord{coordinate1},\ycoord{coordinate1}};
\node at (1,-1.5) {tikz coord 2: \xcoord{coordinate2},\ycoord{coordinate2}};
\node at (1,-2) {tikz coord 3: \xcoord{coordinate3},\ycoord{coordinate3}};
\end{tikzpicture}

\bigskip
\noindent
% Second picture (different scale)
\begin{tikzpicture}

\begin{axis}[
x={(2cm,0)},
y={(0,0.01cm)},
clip = false,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=250,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 6,
ylabel style={rotate=-90},
every axis y label/.style=
{at={(ticklabel* cs:1.02)},anchor=south},
ylabel=$y$,
every axis x label/.style=
{at={(ticklabel* cs:1.02)},below left = 8pt},
every tick/.style={thick},
ytick={0,100,...,200},
xtick={0,1,...,5},
yticklabels={0,100,200},
xlabel=$x$,
xticklabels={0,1,...,5},
minor ytick ={50,150,...,200},
minor xtick ={0.2,0.4,...,5},
tick align=outside]

% Grid
\draw [gray, ystep = 50] (0,0) grid (5,200);

% Should be the same random line as in picture 1
\draw [thick] decorate [
decoration = {random steps,
segment length = 5mm,
amplitude = 3mm}]
{ (\PA) -- (\PB)};
\node at (\PA) [left] {PA};
\node at (\PB) [right] {PB};
\end{axis}
\end{tikzpicture}
\end{varwidth}
\end{document}


Edit: As suggested by cfr I reset the seed for the second picture and get a curve shown below. The curve is similar but not the same. It consists only out of 9 points.

• Is there an option to use a seed for the randomisation? – cfr Dec 25 '15 at 21:45
• In fact, you are already specifying the seed, so the pseudo-random generator will give the same curve every time. – cfr Dec 25 '15 at 21:52
• But you are not actually plotting anything, are you? – cfr Dec 25 '15 at 22:01
• @cfr So you mean I just have to find the right segment length and amplitude for the second plot? What do you mean with: you are not plotting anything? – Nik Dec 25 '15 at 22:05
• You need to reset the seed to 1 (or whatever) before the second picture. I just mean that you aren't plotting anything. You draw the axes but you never plot any function or data or anything else. You draw a picture in the place where you'd normally plot something, but that doesn't make it a plot. – cfr Dec 25 '15 at 22:47

The random seed does not retain its value. Even if it gets changed within the group created by a tikzpicture environment, it will have a different value after that group ends. To ensure the same value for both pictures, set the seed again before the second picture.

For example,

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\pgfmathsetseed{1}
\begin{tikzpicture}
\draw [decorate, decoration={random steps}] (0,0) -- (1,1);
\end{tikzpicture}
%   \pgfmathsetseed{1}
\begin{tikzpicture}
\draw [decorate, red, decoration={random steps}] (0,0) -- (1,1);
\end{tikzpicture}
\end{document}


produces

whereas uncommenting the line to reset the seed to 1 gives

You can do the same within a tikzpicture environment if necessary.

For example,

\documentclass[border=10pt,tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\pgfmathsetseed{1}
\begin{tikzpicture}
\draw [decorate, decoration={random steps}] (0,0) -- (1,1);
%\pgfmathsetseed{1}
\draw [decorate, red, decoration={random steps}] (0,0) -- (1,1);
\end{tikzpicture}
\end{document}


produces

whereas uncommenting the line to reset the seed to 1 gives

Turning to your MWE, when I reset the seed to 1 before the second picture, I get the same shaped path as in the first:

Modified code:

\documentclass[border=10pt]{standalone}
\usepackage{varwidth}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\newcounter{coordinateindex}

% First way to extract coordinate values
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{%
\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}

% Second way to extract coordinate values
\makeatletter
\newcommand\xcoord[2][center]{{%
\pgfpointanchor{#2}{#1}%
\pgfmathparse{\pgf@x/\pgf@xx}%
\pgfmathprintnumber{\pgfmathresult}%
}}
\newcommand\ycoord[2][center]{{%
\pgfpointanchor{#2}{#1}%
\pgfmathparse{\pgf@y/\pgf@yy}%
\pgfmathprintnumber{\pgfmathresult}%
}}
\makeatother

% Making coordinate definitions global
\makeatletter
\pgfplots@start@plot@with@behavioroptions{#1,/pgfplots/.cd,#2}%
\pgfplots@PREPARE@COORD@STREAM{#4}%
\begingroup
\edef\@tempa{{#3}}%
\ifpgfplots@curplot@threedim
\expandafter\endgroup\expandafter
\pgfplots@coord@stream@foreach@threedim\@tempa
\else
\expandafter\endgroup\expandafter
\pgfplots@coord@stream@foreach\@tempa
\fi
}%
\makeatother

% Global coordinates
\def\PA{1,50}
\def\PB{3,150}

% Extract coordinates from path
\tikzset{
put coordinates/.style={
initialize counter/.code={
\setcounter{coordinateindex}{0}
},
initialize counter,
decoration={
show path construction,
moveto code={
\stepcounter{coordinateindex}
\coordinate (#1\thecoordinateindex) at (\tikzinputsegmentfirst);
},
lineto code={
\stepcounter{coordinateindex}
\coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
},
curveto code={
\stepcounter{coordinateindex}
\coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
},
closepath code={
\stepcounter{coordinateindex}
\coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
},
},
postaction={decorate}
},
put coordinates/.default=coordinate
}

\begin{document}
\begin{varwidth}{1.4\linewidth}
% First picture
\pgfmathsetseed{1}
\begin{tikzpicture}
\begin{axis}[
x={(2cm,0)},
y={(0,0.02cm)},
clip = false,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=250,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 6,
ylabel style={rotate=-90},
every axis y label/.style={at={(ticklabel* cs:1.02)}, anchor=south},
ylabel=$y$,
every axis x label/.style={at={(ticklabel* cs:1.02)}, below left = 8pt},
every tick/.style={thick},
ytick={0,100,...,400},
xtick={0,1,...,5},
yticklabels={0,100,200,300,400},
xlabel=$x$,
xticklabels={0,1,...,6},
minor ytick ={50,150,...,350},
minor xtick ={0.2,0.4,...,5},
tick align=outside]
% grid
\draw [gray] (0,0) grid (5,200);
% random line
% getting coordinates from path via: put coordinates
%  information is stored in coordinate1, 2,.., 10 and
%   in the counter \thecoordinateindex
\draw [put coordinates, thick] decorate [decoration = {random steps, segment length = 5mm, amplitude = 3mm}] { (\PA) -- (\PB)};
\node at (\PA) [left = 1mm] {PA};
\node at (\PB) [right= 2mm] {PB};
% printing \thecoordinateindex (=10)
\node at (1,150) {\thecoordinateindex};
\foreach \i in {1,...,10} % Can't use \thecoordinateindex for 10
{
\edef\temp{%
\noexpand \fill (coordinate\i) circle [radius=2pt] node [above=3pt] {\i};
}
\temp
}
\node at (-1.2,200) {axis coord 1:};
\node at (-1.2,160) {axis coord 2:};
\node at (-1.2,120) {axis coord 3:};
\end{axis}
\node at (1,-1) {tikz coord 1: \xcoord{coordinate1},\ycoord{coordinate1}};
\node at (1,-1.5) {tikz coord 2: \xcoord{coordinate2},\ycoord{coordinate2}};
\node at (1,-2) {tikz coord 3: \xcoord{coordinate3},\ycoord{coordinate3}};
\end{tikzpicture}

\bigskip
\noindent
\pgfmathsetseed{1}
% Second picture (different scale)
\begin{tikzpicture}
\begin{axis}[
x={(2cm,0)},
y={(0,0.01cm)},
clip = false,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=250,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 6,
ylabel style={rotate=-90},
every axis y label/.style={at={(ticklabel* cs:1.02)}, anchor=south},
ylabel=$y$,
every axis x label/.style={at={(ticklabel* cs:1.02)}, below left = 8pt},
every tick/.style={thick},
ytick={0,100,...,200},
xtick={0,1,...,5},
yticklabels={0,100,200},
xlabel=$x$,
xticklabels={0,1,...,5},
minor ytick ={50,150,...,200},
minor xtick ={0.2,0.4,...,5},
tick align=outside]
% Grid
\draw [gray, ystep = 50] (0,0) grid (5,200);
% Should be the same random line as in picture 1
\draw [thick] decorate [decoration = {random steps, segment length = 5mm, amplitude = 3mm}] { (\PA) -- (\PB)};
\node at (\PA) [left] {PA};
\node at (\PB) [right] {PB};
\end{axis}
\end{tikzpicture}
\end{varwidth}
\end{document}

• Thank you for clarifying this. I thought there have to be different values for segment length and amplitude for the two pictures when they are given in mm. (I think you uploaded the same picture twice.) So I can work with that and accept as an answer. Nonetheless, why thecoordinateindex is not working? And how can I write a loop for printing/exporting the coordinate values? – Nik Dec 26 '15 at 8:51
• I just checked and I'm getting different values. And if you zoom in in the picture you uploaded you will see that in the first picture the slope at PB is zero but in the second picture not. I try to update my question with a new picture. – Nik Dec 26 '15 at 9:25
• Coordinates are local to the tikzpicture. If you have separate pictures, you cannot use the coordinates from one in the other unless you make the names global using something like remember picture. But, in that case, the coordinates will refer to coordinates in the original picture, even if you use them in the second. So you'd have to also adjust them by transforming them to shift them appropriately. – cfr Dec 26 '15 at 14:34
• It is not going to give exactly the same path, obviously. Because the scale is different. And you can't do it by just adjusting the segment length and amplitude because there's no way to know what to adjust them by since the scaling factor is different for the two axes and you cannot know, given the randomness, how to adjust. Or maybe you can if you just think of it as the line from one point to another. So possibly you could calculate the appropriate factor, given the scaling of the x axis. – cfr Dec 26 '15 at 14:43

I solved the problem for me. The idea is to copy the first curve into the first axis of the second picture. In the second picture there are two axis: one axis with the original scaling and one axis with the new scaling. From the first axis one gets the original coordinates and as we are now in the same tikz picture they doesn't get lost but are usable in the second scaled axis. The first axis with curves and everything will not be drawn or drawn with 0 line thickness.

@ Dr. Manuel Kuehner: random or not doesn't matter. I just want to be sure that in the second picture I'm scaling exact the same curve from the first picture. You may have a look at the links I posted.

\documentclass{standalone}

% To place the pictures beneath
% each other
\usepackage{varwidth}

\usepackage{pgfplots}

\usetikzlibrary{calc}

% Counting points of path
\newcounter{cind}

% Extract coordinate values
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]
{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}

% Making coordinate definitions global
\makeatletter
\pgfplots@start@plot@with@behavioroptions{#1,/pgfplots/.cd,#2}%
\pgfplots@PREPARE@COORD@STREAM{#4}%
\begingroup
\edef\@tempa{{#3}}%
\ifpgfplots@curplot@threedim
\expandafter\endgroup\expandafter
\pgfplots@coord@stream@foreach@threedim\@tempa
\else
\expandafter\endgroup\expandafter
\pgfplots@coord@stream@foreach\@tempa
\fi
}%
\makeatother

% Global coordinates
\def\PA{1, 50}
\def\PB{3,150}

% Extract coordinates from path
\tikzset{
put coordinates/.style={
initialize counter/.code={
\setcounter{cind}{0}
},
initialize counter,
decoration={
show path construction,
moveto code={
\stepcounter{cind}
\coordinate (#1\thecind) at (\tikzinputsegmentfirst);
},
lineto code={
\stepcounter{cind}
\coordinate (#1\thecind) at (\tikzinputsegmentlast);
},
curveto code={
\stepcounter{cind}
\coordinate (#1\thecind) at (\tikzinputsegmentlast);
},
closepath code={
\stepcounter{cind}
\coordinate (#1\thecind) at (\tikzinputsegmentlast);
},
},
postaction={decorate}
},
put coordinates/.default=coordinate
}

\begin{document}

\begin{varwidth}{1.4\linewidth}

% First picture

% Getting the same random curve
\pgfmathsetseed{1}

\begin{tikzpicture}
\begin{axis}[
x={(2cm,0)},
y={(0,0.02cm)},
clip = false,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=250,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 4,
ylabel=$y$,
ytick={0,50,100,...,250},
xtick={0,1,...,4},
yticklabels={0,50,100,...,250},
xlabel=$x$,
xticklabels={0,1,...,4},
]

% Grid
\draw [gray] (0,0) grid (4,250);

% Random line
%  Getting coordinates from path via: put coordinates
%  information is stored in coordinate1, 2,.., 8 and
%   in the counter \thecoordinateindex
\draw [put coordinates, thick] decorate [
decoration = {random steps,
segment length = 6mm,
amplitude = 5mm}]
{ (\PA) -- (\PB)};

\node at (\PA) [left = 1mm] {PA};
\node at (\PB) [right= 2mm] {PB};

% Using here \thecind (for 8) leads to:
%  Package pgf Error: No shape named coordinate0 is known
% edef and noexpand is required within axis
% Put filled circles at every point the index above
\foreach \i in {1,...,8} {
\edef\temp{\noexpand \fill (coordinate\i)
\temp
}
\end{axis}
\end{tikzpicture}

\bigskip
% Second picture (different scale)

% Getting the same random curve
\pgfmathsetseed{1}

\begin{tikzpicture}

% First axis is equivalent to the axis of picture 1
%  but invisible
\begin{axis}[
x={(2cm,0)},
y={(0,0.02cm)},
clip = false,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=250,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 4,
ticks = none,
separate axis lines,
y axis line style= { draw opacity=0, - },
x axis line style= { draw opacity=0, - },
]

% Random line
%  (copy from picture 1, will be invisible)
%  Getting coordinates from path via: put coordinates
%  information is stored in coordinate1, 2,.., 8 and
%   in the counter \thecoordinateindex
\draw [put coordinates, thick] decorate [
decoration = {random steps,
segment length = 6mm,
amplitude = 5mm}]
{ (\PA) -- (\PB)};

\node at (\PA) [left = 1mm] {PA};
\node at (\PB) [right= 2mm] {PB};
\end{axis}

% Second axis for scaling original curve
\begin{axis}[
x={(2cm,0)},
y={(0,0.01cm)},
clip = false,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=250,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 4,
ylabel=$y$,
ytick={0,50,100,...,250},
xtick={0,1,...,4},
yticklabels={0,50,100,150,200,250},
xlabel=$x$,
xticklabels={0,1,...,4},
]

% Grid
\draw [gray, ystep = 50] (0,0) grid (4,250);

% will be invisible
%  in the scaled axis the coordinates of the curve
%   are not scaled
\foreach \i in {1,...,\thecind} {
\edef\temp{\noexpand \fill[blue] (coordinate\i)
\temp
}

% \PA and \PB are scaled to the second axis
\node at (\PA) [left] {PA*};
\node at (\PB) [right] {PB*};
\end{axis}

% To scale the coordinates of the curve
%  one has to extract the x- and y-coordinates first
%   then they are scaled
%    then they are stored in cc1, cc2,.., cc\thecind
\foreach \i  in {1,...,\thecind} {
\ExtractCoordinate{$(coordinate\i)$};
\coordinate (cc\i) at ($(\XCoord, 0.5*\YCoord)$);
}

% Now we can put filled circles at the scaled coordinates
\foreach \i  in {1,...,\thecind}
\fill[orange] (cc\i) circle [radius=2pt] node [above=1pt] {\i};

% Now we can draw a line through the scaled coordinates
\draw [thick] (cc1)
\foreach \i  in {2,...,\thecind} {  -- (cc\i)};

\end{tikzpicture}
\end{varwidth}
\end{document}


Here is an (shorter) approach with exporting the coordinates to a file. In the other approach I had to use \useasboundingbox with the appropriate coordinates.

\documentclass{standalone}
\usepackage{newfile}
\usepackage{verbatim}

\newoutputstream{daaf}
\openoutputfile{daaf.f}{daaf}

% To place the pictures beneath
% each other
\usepackage{varwidth}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotstableset{col sep=comma}

\usetikzlibrary{calc}

% Counting points of path
\newcounter{cind}

% Extract coordinate values
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]
{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}

% Making coordinate definitions global
\makeatletter
\pgfplots@start@plot@with@behavioroptions{#1,/pgfplots/.cd,#2}%
\pgfplots@PREPARE@COORD@STREAM{#4}%
\begingroup
\edef\@tempa{{#3}}%
\ifpgfplots@curplot@threedim
\expandafter\endgroup\expandafter
\pgfplots@coord@stream@foreach@threedim\@tempa
\else
\expandafter\endgroup\expandafter
\pgfplots@coord@stream@foreach\@tempa
\fi
}%
\makeatother

% Global coordinates
\def\PA{1, 50}
\def\PB{3,150}

% Extract coordinates from path
\tikzset{
put coordinates/.style={
initialize counter/.code={
\setcounter{cind}{0}
},
initialize counter,
decoration={
show path construction,
moveto code={
\stepcounter{cind}
\coordinate (#1\thecind) at (\tikzinputsegmentfirst);
},
lineto code={
\stepcounter{cind}
\coordinate (#1\thecind) at (\tikzinputsegmentlast);
},
curveto code={
\stepcounter{cind}
\coordinate (#1\thecind) at (\tikzinputsegmentlast);
},
closepath code={
\stepcounter{cind}
\coordinate (#1\thecind) at (\tikzinputsegmentlast);
},
},
postaction={decorate}
},
put coordinates/.default=coordinate
}

\begin{document}

\begin{varwidth}{1.4\linewidth}

% First picture

% Getting the same random curve
\pgfmathsetseed{1}

\begin{tikzpicture}
\begin{axis}[
x={(2cm,0)},
y={(0,0.02cm)},
clip = false,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=250,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 4,
ylabel=$y$,
ytick={0,50,100,...,250},
xtick={0,1,...,4},
yticklabels={0,50,100,...,250},
xlabel=$x$,
xticklabels={0,1,...,4},
]

% Grid
\draw [gray] (0,0) grid (4,250);

% Random line
%  Getting coordinates from path via: put coordinates
%  information is stored in coordinate1, 2,.., 8 and
%   in the counter \thecoordinateindex
\draw [put coordinates, thick] decorate [
decoration = {random steps,
segment length = 6mm,
amplitude = 5mm}]
{ (\PA) -- (\PB)};

\node at (\PA) [left = 1mm] {PA};
\node at (\PB) [right= 2mm] {PB};

% Using here \thecind (for 8) leads to:
%  Package pgf Error: No shape named coordinate0 is known
% edef and noexpand is required within axis
% Put filled circles at every point the index above
\foreach \i in {1,...,8} {
\edef\temp{\noexpand \fill (coordinate\i)
\temp
}
\end{axis}

\foreach \i in {1,...,8} {
\ExtractCoordinate{$(coordinate\i)$}
\pgfmathsetmacro\mx{(\XCoord)/1cm/2} % x={(2cm,0)} /2
\pgfmathsetmacro\my{(\YCoord)/1cm/0.02} % y={(0,0.02cm)} /0.02
}
\end{tikzpicture}

\closeoutputstream{daaf}

\verbatiminput{daaf.f}

\begin{tikzpicture}
\begin{axis}[
x={(4cm,0)},
y={(0,0.1cm)},
clip = true,
axis y line=left,
axis x line=left,
ymin=0,     % start the diagram at this y-coordinate
ymax=100,   % end   the diagram at this y-coordinate
xmin = 0,
xmax = 2,
ylabel=$y$,
ytick={0,50,100},
xtick={0,1,2},
yticklabels={0,50,100},
xlabel=$x$,
xticklabels={0,1,2},
]
\addplot [blue,thick] table [x index=0,y index=1]  {daaf.f};

% Grid
\draw [gray] (0,0) grid (2,100);

\node at (\PA) [left = 1mm] {PA};
% \node at (\PB) [right= 2mm] {PB};
\end{axis}
\end{tikzpicture}
\end{varwidth}
\end{document}