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?
coordinates
read literal coordinate expressions but A,B are TikZ referrals. Maybe simply\path[name path=B] (A) -- (B);
?\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.\path
in my real case use; it didn't cooperated well with somesoft clip
options, but I can live with that :). WOuld you write an answer?\addplot
s in anafter end axis/.code={}
.A
". And what problems did you have with thesoft 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 toname path
B instead of using percusse's solution.