Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addplot: set default domain

Tags:

latex

tikz

pgf

In pgfplots, when plotting a function, the default domain in -5:5. I would like to set this to be xmin:xmax by default. Is there a way to do this? In other words, I would like to be able to write

\addplot {x^2};

instead of

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

To be more specific, here is a MWE of what I look for (and does not work):

\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}
like image 533
LMBC Avatar asked Dec 06 '25 02:12

LMBC


1 Answers

\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

like image 115
M. Al Jumaily Avatar answered Dec 08 '25 22:12

M. Al Jumaily



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!