7

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:

\documentclass[border=3pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{arrows.meta}

\makeatletter
\pgfplotsset{
    /tikz/max node/.style={
        anchor=south,
    },
    mark max/.style={
        point meta rel=per plot,
        visualization depends on={x \as \xvalue},
        scatter/@pre marker code/.code={%
        \ifx\pgfplotspointmeta\pgfplots@metamax
            \def\markopts{}%
            \coordinate (maximum);
            \node [max node] {
                \pgfmathprintnumber[fixed]{\xvalue},%
                \pgfmathprintnumber[fixed]{\pgfplotspointmeta}
            };
        \else
            \def\markopts{mark=none}
        \fi
            \expandafter\scope\expandafter[\markopts]
        },%
        scatter/@post marker code/.code={%
            \endscope
        },
        scatter
    }
}
\makeatother

\pgfmathdeclarefunction{cubic}{1}{%
  \pgfmathparse{-2*(#1+2)*(#1+2)*(#1-2)+40}%
}

\begin{document}        

\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  xtick={-2.5,2.5},
  ytick={\empty},
  domain=-2.5:2.5,
  ymin=-1,
  xmin=-4,
  xmax=3,
  clip=false,
] 
% The curve
\addplot [mark max,draw=red,name path=A] plot {cubic(x)};
\makeatletter
\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
\makeatother
\end{axis}
\end{tikzpicture}

\end{document}

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 '15 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 '15 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? – Gonzalo Medina Aug 16 '15 at 18:15
  • @Jake Thanks. Would it be possible to somehow use \addplots in an after end axis/.code={}. – Gonzalo Medina Aug 16 '15 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. – Stefan Pinnow Feb 18 '17 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.