# [Tex/LaTex] Plotting function on limited scale with tikz

tikz-pgf

I want to plot few functions with tikz. Values of some of them grows too huge. In tikz manual there is an example with "nice" functions that do not grow too much:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=red] plot (\x,\x) node[right] {$f(x) =x$};
\draw[color=blue] plot (\x,{sin(\x r)}) node[right] {$f(x) = \sin x$};
\draw[color=orange] plot (\x,{0.05*exp(\x)}) node[right] {$f(x) =q\frac{1}{20} \mathrm e^x$};
\end{tikzpicture}
\end{document}


How to modify this so that only y-values from -1 to 4 are (still) shown, but instead of 0.05*e^x there is e^x shown?

I know that this is quite impossible if function oscillates. But let's suppose that we are plotting function with one local minimun or no minimun at all.

Is this what you seek? clip technique is used to cut the curve exceeds the window generated by clip. Please note that when clip is used, the clipping effect should be limited within the scope. Without scope the clipping will continue to take effect all the way down in the code.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[domain=0:4]
\begin{scope}                           % scope environment
\clip  (-1,-1.1) rectangle (6.9,4.9);   % This is the clip window determined by user.
\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=red] plot (\x,\x) node[right] {$f(x) =x$};
\draw[color=blue] plot (\x,{sin(\x r)}) node[right] {$f(x) = \sin x$};
\draw[color=orange] plot (\x,{exp(\x)}) node at (1,4) {$f(x) = \mathrm e^x$};
\end{scope}                             % scope envirnment
\end{tikzpicture}
\end{document}


Update: A draw based on PGFPLOTS is added for reference.

Code

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}

\begin{tikzpicture}
\begin{axis}[domain=0:4, samples=100,grid=major,
restrict y to domain=-1:4,xlabel=$x$,ylabel=$f(x)$, legend pos=north west]
\legend{$x$,$\sin(x)$,$e^x$}