# How can I add another plot to the \addplot command in PGFPlots?

My goal is to add another plot to the \addplot command and this small example hopefully shows what I'm trying to do:

\documentclass{standalone}
\usepackage{pgfplotstable}
\begin{document}

procs, none
16, 6.079748e+03
32, 3.624823e+03
64, 1.685646e+03
128, 9.180763e+02
256, 5.977239e+02
512, 4.410280e+02
}\scalability

\begin{tikzpicture}
\begin{loglogaxis}[
ylabel={Time (sec)},
xtick=data,
xticklabels from table={\scalability}{procs},
table/x=procs,
set scaling/.code={
\edef\xcol{\pgfkeysvalueof{/pgfplots/table/x}}
\pgfplotstablegetelem{0}{\xcol}\of{\scalability}
\edef\proc{\pgfplotsretval}
\pgfplotstablegetelem{0}{\pgfkeysvalueof{/pgfplots/y}}\of{\scalability}
\edef\offset{\pgfplotsretval}
\pgfplotsset{table/y expr=\offset * \proc / \thisrowno{0}}
\pgfplotsextra{
\draw (1,1) -- (256,256);              % this is drawn
\addplot table[y=none] {\scalability}; % this is not
}
},
scaling/.style={
forget plot,
set scaling,
},
forget plot style={opacity=0.4},
]

\end{loglogaxis}
\end{tikzpicture}

\end{document}


What I'd like to see is:

but tinkering with the .code functionality (and also execute at end plot) nothing seems to be able to call another \addplot only lower level TikZ commands (\draw, \path, etc.):

Is there any way to get another \addplot appended?

-
I think this is going to be quite hard. It would probably be easier to define a new command that calls both of the plots. Do you have a particular reason for not wanting that? –  Jake Jun 19 '12 at 21:31
Well, for starters, the execute and .code keys seemed like the elegant way to do this. Also, if defining a \newcommand, it seemed quite hacky to pass optional key-value pairs down to pgfplots; i.e. \mynewplotcommand +[optional styles to append]; \mynewplotcommand [optional styles to override] If passing that down to pgfplots is easier than it seems, then it would indeed work. –  Sean Farley Jun 20 '12 at 0:02

Your approach does not work because \addplot is designed to be placed within the axis -- and \pgfplotsextra is designed to NOT process things inside of an axis (but to postpone their processing).

If you want to use \addplot, you have to place it into the axis.

But from what I understand from your application, I believe that you can simply place your expression evaluation stuff directly in front of the plot in question:

\documentclass{standalone}
\usepackage{pgfplotstable}
\begin{document}

procs, none
16, 6.079748e+03
32, 3.624823e+03
64, 1.685646e+03
128, 9.180763e+02
256, 5.977239e+02
512, 4.410280e+02
}\scalability

\begin{tikzpicture}
\begin{loglogaxis}[
ylabel={Time (sec)},
xtick=data,
xticklabels from table={\scalability}{procs},
table/x=procs,
table/y=none,
forget plot style={opacity=0.4},
]

\pgfplotstablegetelem{0}{\pgfkeysvalueof{/pgfplots/table/x}}\of{\scalability}
\edef\proc{\pgfplotsretval}
\pgfplotstablegetelem{0}{\pgfkeysvalueof{/pgfplots/table/y}}\of{\scalability}
\edef\offset{\pgfplotsretval}

\addplot +[forget plot] table[y expr=\offset * \proc / \thisrowno{0}] {\scalability};

\end{loglogaxis}
\end{tikzpicture}

\end{document}


This seems to do exactly the same job as in your request - without the key definition overhead. It works because your temporaries are immediately evaluated by \addplot; they are not postponed (because they are necessary to evaluate coordinates).

You may want to eliminate the \pgfkeysvalueof{/pgfplots/table/x} statements to improve the robustness (your example did not properly define table/y).

-
Ah, sorry I was clear :-( I was trying to do all of the above with just one command. Part of me wanted to test the capabilities of /.code and execute ... but it seems those will not do the job, correct? If that is true, then is there a way to use \newcommand that passes pgf keys (as I try to illustrate above in my comment to @Jake)? –  Sean Farley Jun 21 '12 at 18:15
Well, macros are powerful - you can surely write some top level macro which starts two separate \addplot commands (i.e. by extracting the code of my example). However, \addplot inside of \addplot is unsupported - and \addplot within \pgfplotsextra as well (as explained in my answer). –  Christian Feuersänger Jun 23 '12 at 7:53