pgfplots:标记函数的最大值/最小值

pgfplots:标记函数的最大值/最小值

如何在 pgfplots 中标记 x^2 等函数的最大值或最小值?

\begin{tikzpicture}
   \begin{axis}
      \addplot+[mark=none] plot {x^2};
   \end{axis}
\end{tikzpicture}

答案1

您可以使用该scatter机制来实现这一点,该机制允许您在绘制每个标记之前执行代码。如果您设置了point meta rel=per plot,则宏\pgfplots@metamin包含最小值meta,并\pgfplots@metamax包含最大值meta。默认情况下,该meta值与该值相同y

我定义了两种新样式,mark min它们mark max将分别关闭除最小值和最大值之外的所有绘图标记。

如果您还想打印极值坐标,则需要使用 提供 x 坐标visualization depends on=<value> \as <\macroname>

\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{}%
                \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}
    \addplot +[mark min, every node near coord/.style=] plot {x^2};
    \addplot +[mark max] plot {-x^2+5*x+5};
\end{axis}
\end{tikzpicture}

\end{document}

答案2

这不是问题的答案,而是pgf 2.1 CVS并且没有pgfplots(为什么?)我们可以用数据可视化库.也许可以使用pgf图有了这个库。

实际上我不知道如何添加杰克的风格。

在此处输入图片描述

更新代码我清理了代码

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{
  datavisualization,
  datavisualization.polar,% not useful here but interesting to know
  datavisualization.formats.functions}

\begin{document}  

  \pgfdvdeclarestylesheet{my colors}
{
  default style/.style={visualizer color=black},
  1/.style={visualizer color=blue},
  2/.style={visualizer color=red},
}

\begin{tikzpicture}[baseline,scale=2]
  \datavisualization [ scientific axes=clean,
                       visualize as smooth line/.list={f,g},% not sin and cos
                       style sheet=my colors,
                       f={label in legend={text=$f(x)$}},
                       g={label in legend={text=$g(x)$}},
                       data/format=function ]
  data [set=f] {
    var x : interval [-5:5];
    func y =  \value x * \value x;
  } 
    info {
    \draw [blue,fill=blue!20,] (visualization cs: x={(0)}, y=0) circle [radius=1pt]
      node [below,font=\footnotesize] {min point};
  }
  data [set=g] {
    var x : interval [-5:5];
    func y = - \value x * \value x +5* \value x +5;
  } 
      info {
    \draw [red,fill=red!20] (visualization cs: x={(2.5)}, y=11.25) circle [radius=1pt]
      node [above,font=\footnotesize] {max point};
  }  ;
\end{tikzpicture}

\end{document} 

相关内容