1

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}

enter image description here The left picture with linear scale is perfect. In the right picture with logarithmic scale the value is not written correct.

Stefan Pinnow
  • 29,535

0 Answers0