0

I am trying to automate min/max values using the code here: How can I automatically mark local extrema with pgfplots and scatter?

  1. The horizontal line appears at height of the label instead of the points (in MWE2)
  2. Numerical issues regarding finding of min/max values on tikz are elicited here (in MWE3) so I guess I could go back to the > 500pt concept. An alternative that could be interesting is to only label one of the minima, say the first one.

I tried to introduce \ifx\first\undefined and \newbool{first} with true false switches but it does not affect the markings.

Only the first would already be something but even better if I could choose them.

\documentclass[preview=false,tikz=true]{standalone}
\usepackage{pgfplots}
\usepackage{etoolbox}

\newbool{isFirst}

\makeatletter \pgfplotsset{ /tikz/max node/.style={ anchor=south, }, /tikz/min node/.style={ anchor=north, name=minimum }, mark min/.style={ point meta rel=per plot, visualization depends on={x \as \xvalue}, scatter/@pre marker code/.code={% \ifx\pgfplotspointmeta\pgfplots@metamin \ifbool{isFirst}{ %\boolfalse{isFirst} % this does not actually disable next labels \def\markopts{}% \coordinate (minimum); \node [min node] { \pgfmathprintnumber[fixed]{\xvalue},% \pgfmathprintnumber[fixed]{\pgfplotspointmeta} }; } { \def\markopts{mark=none} } \else \def\markopts{mark=none} \fi \expandafter\scope\expandafter[\markopts,every node near coord/.style=green] },% scatter/@post marker code/.code={% \endscope }, scatter, }, 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

\begin{document} \booltrue{isFirst} \begin{tikzpicture} \begin{axis}[ domain=-100:100, axis lines*=middle,after end axis/.code={ \draw [thick, dashed, gray] (maximum) --({axis cs:0,0}-|maximum); } ]
\addplot +[mark max,samples=5000] plot {sin(x)}; \end{axis} \end{tikzpicture}

\booltrue{isFirst}
\begin{tikzpicture}
    \begin{axis}[
        domain=-100:100,
        axis lines*=middle
        ,after end axis/.code={
            \draw [thin, loosely dashed, gray] (minimum) -- ({rel axis cs:0,0}|-minimum);
            \draw [thin, loosely dashed, gray] (minimum) -- ({rel axis cs:1,0}|-minimum);
        }
        ]       
        \addplot +[mark min,samples=5000] plot {sin(8*x)};
    \end{axis}
\end{tikzpicture}

\booltrue{isFirst}
\begin{tikzpicture}
    \begin{axis}[
        domain=-100:100,
        axis lines*=middle
        ]       
        \addplot +[mark min,samples=5000] plot {sin(50*x)};
    \end{axis}
\end{tikzpicture}

\end{document} ``

Juan Castaño
  • 28,426

0 Answers0