I would like to mark the maximum or minimum value of a function in pgfplots. For linear scale this question has been answered by Jake. How to adapt/generalize this also for logarithmic scale?
\documentclass{standalone}
\usepackage{pgfplots}
\makeatletter
\pgfplotsset{
/tikz/max node/.style={
anchor=south
},
/tikz/min node/.style={
anchor=north
},
mark min/.style={
point meta rel=per plot,
visualization depends on={x \as \xvalue},
scatter/@pre marker code/.code={%
\ifx\pgfplotspointmeta\pgfplots@metamin
\def\markopts{}%
\pgfmathsetmacro\result{}
\node [min node] {
\pgfmathprintnumber[fixed]{\xvalue},%
\pgfmathprintnumber[fixed]{\pgfplotspointmeta}
};
\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{}%
\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}
\begin{tikzpicture}
\begin{axis}[
xmin=1,xmax=4]
\addplot +[mark min, every node near coord/.style=] plot {5+(x-2)^2};
\addplot +[mark max] plot {-x^2+5x+5};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[ymode=log,
xmin=1,xmax=4]
\addplot +[mark min, every node near coord/.style=] plot {5+(x-2)^2};
\addplot +[mark max] plot {-x^2+5x+5};
\end{axis}
\end{tikzpicture}
\end{document}
The left picture with linear scale is perfect. In the right picture with logarithmic scale the value is not written correct.
\pgfplotspointmetas with\pgfkeysvalueof{/data point/y}s. – Stefan Pinnow Dec 06 '23 at 15:38\addplot +[mark min, every node near coord/.style=,samples=501] plot {0.0005+(x-2)^2};How can this be fixed? – Michael Gfrerer Dec 07 '23 at 07:28fixedformat forpgfmathprintnumber. Change it e.g. tostd,precision=2,zerofill. – Stefan Pinnow Dec 07 '23 at 09:56