3

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}
LMBC
  • 76
  • 1
  • 8

1 Answers1

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

M. Al Jumaily
  • 731
  • 1
  • 6
  • 21
  • Thanks M. Al Jumaily. I don't think I was sufficiently clear about what I was lookign for, so I edited my question with a specific MWE of what I'm looking for and was unable to find. Basically, I want domain to be xmin:xmax by default when addplot'ing an expression. – LMBC Aug 13 '18 at 14:45
  • 1
    I think your question was clear at first and even better now. I basically declared two variables `Min` and `Max` to denote the domain and set their values -10 and 10 respectively (you can change the values to whatever you would like). I then created two plots without specifying the domain of the plots i.e. didn't use `xmin=-10, xmax=10` and automatically the domain for both plots became -10...10. I hope that helps, if not, what values of xmin:xmax would you like to use? Would you like to have the same domain for all plots without specifying the domain explicitly for each plot correct? – M. Al Jumaily Aug 13 '18 at 15:43
  • Basically, I would like every `\addplot` in a given axis to pick up the values of `xmin` and `xmax` declared in that axis command and use `xmin:xmax` as domain, without the need to declare such domain for each addplot. When you plot data from a file, for example, it's understood that the domain is xmin:xmax. However, when plotting a funciton the domain is `-5:5` unless otherwise specified. This is what I would like to do: to make `xmin:xmax` the default domain just like in data plots. Thanks for listening! – LMBC Aug 13 '18 at 16:13
  • I hope I understood correctly. When you import data from a file, the `xmin:xmax` is automatically chosen based on the smallest value `xmin` and the highest value `xmax` from the data. This is because your data is finite. The program surely knows what is the min and max values because the values are finite and one loop is required to find the min and max values. When it comes to something like `x^2`, your data points is infinite (i.e. x∈R). There is no explicit min nor max value since the domain is (-∞,∞). By default, -5:5 is the domain chosen. My solution enables you to choose your own values. – M. Al Jumaily Aug 13 '18 at 16:42
  • In my MWE, I declare the x axis as `\begin{axis}[xmin=-10, xmax=10]`. I understand that the domain of `x^2` is R, but now that I've declared `xmin=-10, xmax=10` for the current axis, why can't `\addplot` simply take this as the domain? – LMBC Aug 13 '18 at 18:05
  • You can set `domain = -10:10` in the axis options, which will propagate to the plots. – Anshul Singhvi Dec 10 '19 at 19:14