# How can I limit the length of a plotted function?

I'm trying to plot a simple exponential function on a graph, however, the function itself isn't staying within the domain.

Here's the code

\begin{tikzpicture}
\draw[->] (-3,0) -- (3,0) node[right] {$x$};
\draw[->] (0,-3) -- (0,3) node[above] {$y$};
\draw[domain=-3:3, variable=\x, red] plot ({\x}, {exp(\x)}) node[right] {$e^x$};
\end{tikzpicture}


### Option 1: Limit the domain

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-3,0) -- (3,0) node[right] {$x$};
\draw[->] (0,-3) -- (0,3) node[above] {$y$};
\draw[domain=-3:1, variable=\x, red] plot ({\x}, {exp(\x)}) node[right] {$e^x$};
\end{tikzpicture}
\end{document}


### Option 2: Clip it

Disadvantage: Things outside the clipping area (here the label) just disappear.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-3,0) -- (3,0) node[right] {$x$};
\draw[->] (0,-3) -- (0,3) node[above] {$y$};
\clip (-3,-3) rectangle (3,3);
\draw[domain=-3:3, variable=\x, red] plot ({\x}, {exp(\x)}) node[right] {$e^x$};
\end{tikzpicture}
\end{document}


### Option 3: Use pgfplots

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
domain=-3:3,
xmin=-3, xmax=3,
ymin=-3, ymax=3,
xlabel={$x$},
ylabel={$y$},
restrict y to domain=-3:3,
]
\addplot+[no marks] {exp(x)} node[right] {$e^x$};
\end{axis}
\end{tikzpicture}
\end{document}


• Makes sense to use domain=-3:1 since exp(1)=e is approximately 2.71828, less than 3. Nov 28, 2017 at 23:12