addplot:设置默认域

时间:2018-08-12 20:44:15

标签: latex tikz pgf

pgfplots 中,当绘制函数时,是-5:5中的默认域。我想将其默认设置为xmin:xmax。有没有办法做到这一点?换句话说,我希望能够写

\addplot {x^2};

代替

\addplot[domain=xmin:xmax] {x^2};

更具体地说,这是我所寻找的MWE(不起作用):

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{domain = min:max}
\begin{document}

    \centering
    \begin{tikzpicture}
        \begin{axis}[xmin=-10, xmax=10, xlabel = $x$, ylabel = {$f(x) = x^2$}]
            \addplot{x^2};
        \end{axis}
    \end{tikzpicture}
\end{document}

1 个答案:

答案 0 :(得分:2)

\documentclass{article}
\usepackage{pgfplots}

%What you really need!
\newcommand\Min{-10}
\newcommand\Max{10}
\pgfplotsset{domain = \Min : \Max}

\begin{document}
    \centering
    \begin{tikzpicture}%x^2
        \begin{axis}[xlabel = $x$, ylabel = {$f(x) = x^2$}]
            \addplot{x^2};
        \end{axis}
    \end{tikzpicture}

    \begin{tikzpicture}%-(x^2)
        \begin{axis}[xlabel = $x$, ylabel = {$f(x) = -(x^2)$}]
            \addplot{-x^2};
        \end{axis}
    \end{tikzpicture}
\end{document}

Output