[Tex/LaTex] Help with grid lines in a pgfplot (Version 3)

pgfplotstikz-pgf

The following display is that of a line and of a parabola on the Cartesian plane with grid lines. The grid lines are drawn nicely … but to draw the grid lines I have included the axis options xtick={-7,-6,...,7}, and ytick={-5,-4,...,6},, and these options print all the numbers along the axes which makes a mess. Except for -3 and 3 along the x-axis and 4 along the y-axis, I would like all the labels for the tick marks removed. (Since I have a grid, I guess the tick marks themselves are redundant.)

I suppose that I don't need the tick marks or the labels -3, 3, and 4 for the tick marks printed. I could put the coordinates (-3,0), (3,0) and (0,4) in nodes and use the option fill=white. Any aesthetic suggestions would be appreciated.

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}

\begin{axis}[grid=both,grid style={line width=.1pt, draw=gray!10},major grid style={line width=.1pt,draw=gray!10}, clip=false,
    xmin=-7,xmax=7,
    ymin=-5,ymax=6,
    axis lines=middle,
    enlargelimits={abs=0.5},
    xtick={-7,-6,...,7},ytick={-5,-4,...,6},
    xticklabel style={fill=none},
    yticklabel style={fill=none},
    extra x ticks={-3,3},
    extra x tick labels={\,$-3$, $3$},
    extra x tick style={x tick label style={font=\tiny,fill=white}},
    extra y ticks={4},
    extra y tick labels={$4$},
    extra y tick style={y tick label style={above left, font=\tiny,fill=white}},
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[latex-latex,samples=2,domain=-6.75:1.5] {(4/3) * x + 4} node[anchor=south east,pos=0.1,fill=white,font=\footnotesize]{$y = f(x)$};
\addplot[samples=201,domain=-4.5:4.5] {-(4/9) * x^2 + 4} node[anchor=south west,pos=0.6,fill=white,font=\footnotesize]{$y = g(x)$};

\end{axis}
\end{tikzpicture}

\end{document}

Best Answer

One way you could do it is to write:

xticklabels={,,,,-3,,}

and so on. Of course this is tedious and long to do. The method you were using was mostly correct: using only the extra tick labels to draw the ones you need, although you don't need \, in them.

If you want to make the regular labels empty, you have two ways of doing it, both valid (I used them both in my code):

xticklabels={\empty},
yticklabels={,,},

Also, you can apply the style to the tick labels. You can either set a style to the regular and extra tick labels independently, or you can use the regular yticklabel style={}, and it will apply to both.

Output

figure 1

Code

\documentclass{amsart}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{calc}

\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=both,grid style={line width=.1pt, draw=gray!10},major grid style={line width=.1pt,draw=gray!10}, clip=false,
    xmin=-7,xmax=7,
    ymin=-5,ymax=6,
    axis lines=middle,
    enlargelimits={abs=0.5},
    xtick={-7,-6,...,7},
    ytick={-5,-4,...,6},
    xticklabels={\empty},
    yticklabels={,,},
    extra x ticks={-3,3},
    extra x tick labels={$-3$, $3$},
    extra y ticks={4},
    extra y tick labels={$4$},
    yticklabel style={anchor=south east},
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[latex-latex,samples=2,domain=-6.75:1.5] {(4/3) * x + 4} node[anchor=south east,pos=0.1,fill=white,font=\footnotesize]{$y = f(x)$};
\addplot[samples=201,domain=-4.5:4.5] {-(4/9) * x^2 + 4} node[anchor=south west,pos=0.6,fill=white,font=\footnotesize]{$y = g(x)$};

\end{axis}
\end{tikzpicture}
\end{document}