2

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

e^x graph

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}

Thanks for any help you can give!

0

1 Answer 1

4

Option 1: Limit the domain

Disadvantage: You have to think about where is a sensible cutoff.

\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}

enter image description here

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}

enter image description here

Option 3: Use pgfplots

Disadvantage: Quite verbose.

\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}

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .