Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
4  
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 Dec 2 '13 at 12:43
1  
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. –  Jubobs Dec 2 '13 at 12:44
    
@Jubobs: Done, sorry for forgetting. –  Jori Mäntysalo Dec 2 '13 at 13:41
    
@Jake: No, nothing particular reason. Every LaTeX-only solution is OK. –  Jori Mäntysalo Dec 2 '13 at 13:42
add comment

1 Answer

up vote 3 down vote accepted

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.

enter image description here

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

enter image description here

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]
\addplot [color=red]  {x};
\addplot [color=blue] {sin(deg(x)};
\addplot [color=green]{exp(x)};
\legend{$x$,$\sin(x)$,$e^x$}
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
I mark this as accepted. However, it seems that pgfplots will be better approach, due to “Dimension too large”-errors with tikz. –  Jori Mäntysalo Dec 3 '13 at 8:57
    
@JoriMäntysalo -- To that end, I add functional plots using pfgplots for your reference. –  Jesse Dec 3 '13 at 12:28
    
@Jori Mäntysalo, as I know, tikz is for "painting", and pgfplots is for "plotting". –  selwyndd21 Dec 3 '13 at 15:53
    
If I could, I would mark another answer from Jesse to another accepted answer. Thanks! –  Jori Mäntysalo Dec 5 '13 at 18:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.