# Plotting function on limited scale with tikz

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.

• This would be really simple with PGFPlots (which is a package that uses PGF/TikZ for creating plots): You'd simply say restrict y to domain=-1:4. Do you have a particular reason for wanting to draw your plots using "raw" TikZ?
– Jake
Commented Dec 2, 2013 at 12:43
• Please make your code compilable, starting with \documentclass{...} and ending with \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to help you. Help them help you: remove that one hurdle between you and a solution to your problem. Commented Dec 2, 2013 at 12:44
• @Jubobs: Done, sorry for forgetting. Commented Dec 2, 2013 at 13:41
• @Jake: No, nothing particular reason. Every LaTeX-only solution is OK. Commented Dec 2, 2013 at 13:42

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