用对数刻度标记函数的最大值/最小值

用对数刻度标记函数的最大值/最小值

我想在 pgfplots 中标记函数的最大值或最小值。对于线性比例,这个问题已由杰克. 如何适应/推广这对对数尺度?

\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+5*x+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+5*x+5};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述 左图采用线性刻度,显示完美。右图采用对数刻度,显示值不正确。

相关内容