I'm using Jake's code in his answer to How can I automatically mark local extrema with pgfplots and scatter?. Using the mark max style I can retrieve the value for the y-coordinate of the maximum for a function using \pgfplots@metamax.

Having this value I can use it, for example, to add nodes using the axis cs coordinate system, as the code below shows; however, I cannot use those nodes as coordinates for a new \addplot (uncommet the commented-out line with \addplot and you'll get an error).

How can I use those named nodes for an \addplot?

The code:


    /tikz/max node/.style={
    mark max/.style={
        point meta rel=per plot,
        visualization depends on={x \as \xvalue},
        scatter/@pre marker code/.code={%
            \coordinate (maximum);
            \node [max node] {
        scatter/@post marker code/.code={%



  axis lines=middle,
% The curve
\addplot [mark max,draw=red,name path=A] plot {cubic(x)};
\node (A) at (axis cs:-2.5,\pgfplots@metamax) {A};
\node (B) at (axis cs:2.5,\pgfplots@metamax) {B};
%\addplot[name path=B] coordinates {(A) (B)};% doesn't work


Perhaps I am using the wrong approach here. What I want to do is what follows:

I have the graph of a function A drawn using \addplot; this path is named using name path=A. I need to add another \addplot (that will be named B) defined by two points whose x-coordinates are known but whose y-coordinates are calculated adding positive or negative shifts to the y-coordinate for the maximum of A. Then I will use the fillbetween library to fill the area between A and B. How can this be done?

  • 3
    coordinates read literal coordinate expressions but A,B are TikZ referrals. Maybe simply \path[name path=B] (A) -- (B); ?
    – percusse
    Aug 16, 2015 at 8:16
  • 1
    I think the problem here is that \pgfplots@metamax isn't fixed until all the \addplot commands have been evaluated. The \node commands are stored and only executed after the \addplot evaluation is complete, independently of whether they're placed before or after the \addplot commands in the code.
    – Jake
    Aug 16, 2015 at 10:39
  • 1
    @percusse Yes, I ended up using a simple \path in my real case use; it didn't cooperated well with some soft clip options, but I can live with that :). WOuld you write an answer? Aug 16, 2015 at 18:15
  • @Jake Thanks. Would it be possible to somehow use \addplots in an after end axis/.code={}. Aug 16, 2015 at 18:16
  • @GonzaloMedina, sorry, I didn't get what exactly you mean with "adding positive or negative shifts to the y-coordinate for the maximum of A". And what problems did you have with the soft clip option? As far as I can follow I don't think that these problems weren't there if you would have used the \addplot command to name path B instead of using percusse's solution. Feb 18, 2017 at 11:56


You must log in to answer this question.

Browse other questions tagged .