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?
coordinatesread literal coordinate expressions but A,B are TikZ referrals. Maybe simply\path[name path=B] (A) -- (B);? – percusse Aug 16 '15 at 08:16\pgfplots@metamaxisn't fixed until all the\addplotcommands have been evaluated. The\nodecommands are stored and only executed after the\addplotevaluation is complete, independently of whether they're placed before or after the\addplotcommands in the code. – Jake Aug 16 '15 at 10:39\pathin my real case use; it didn't cooperated well with somesoft clipoptions, but I can live with that :). WOuld you write an answer? – Gonzalo Medina Aug 16 '15 at 18:15\addplots in anafter end axis/.code={}. – Gonzalo Medina Aug 16 '15 at 18:16A". And what problems did you have with thesoft clipoption? As far as I can follow I don't think that these problems weren't there if you would have used the\addplotcommand toname pathB instead of using percusse's solution. – Stefan Pinnow Feb 18 '17 at 11:56