[Tex/LaTex] Graph showing functions over the same x and y ranges

plottikz-pgf

I want to draw functions and have used this code. But I don't like to give every function a separate domain, I want to say this function has ymax=4 and ymin=-3.
Who can help me?

enter image description here

\begin{tikzpicture}[scale=1, >=latex]
\draw[very thin, color=black, dotted, step=1 cm] (-7,-5) grid (7,5);
\draw[->] (-7.5,0) -- (7.5,0) node[right] {\scriptsize $x$};
\draw[->] (0,-5) -- (0,5.5) node[above] {\scriptsize $f(x)$};
\foreach \x in {-7,-6,...,7} \draw (\x, 1pt) -- (\x,-3pt) node[anchor=north] {\tiny \x};
\foreach \y in {-5,-4,...,5} \draw (1pt,\y) -- (-3pt,\y) node[anchor=east] {\tiny \y};
\draw[color=black, very thick, domain=-2.2:2.1, samples=100]   plot (\x,{-1/3*(\x)^2})
  node[right] {\tiny $p_1$};
\draw[color=black, dashed, domain=-1.1:1.1, samples=100]   plot (\x,{1/6*(\x)^2})
    node[right] {\tiny $p_2$};
\draw[color=black, thick, domain=-2.2:2.2, samples=100]   plot (\x,{-(\x)^2})
    node[right] {\tiny $p_3$};
\draw[color=black, thin, samples=100]   plot (\x,{0.1*(\x)^2}) node[right] {\tiny $p_4$};
\end{tikzpicture}

Best Answer

For this type of graph, I recommend using the pgfplots package over tikz, which is limited in terms of control over axis range and function domain. The code below should get you started with pgfplots.

Edit: Use the minor x tick num = n key (and equivalents for y- and z-axes) to specify how many minor ticks ('n', here) you want between two successive major ticks. Be mindful of potential off-by-one errors: n minor ticks translate to n+1 subintervals between two successive major ticks.

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}[scale=1.7]
    \begin{axis}%
        [
            grid=major,  
            xtick={-7,-6,...,7},  
            minor x tick num=4, % 4 minor ticks => 5 subintervals
            xmin=-6,
            xmax=6,
            xlabel={\scriptsize $x$},
            axis x line=middle,
            ytick={-5,-4,...,5},
            minor y tick num=4,  % 4 minor ticks => 5 subintervals
            ymin=-3,
            ymax=4,
            ylabel={\scriptsize $f(x)$},
            axis y line=middle,
            no markers,
            samples=100,
            domain=-6:6,
        ]
        \addplot (x,{-1/3*x^2});
        \addplot (x,{1/6*x^2});
        \addplot (x,{-x^2});
        \addplot (x,{0.1*x^2});
    \end{axis}
\end{tikzpicture}
\end{document}
Related Question