# Grid display problem in Tikz and Pgfplots

I'm having trouble displaying some of my grids. Here is an MWE to illustrate :

\documentclass[12pt]{book}
\usepackage{tikz,pgfplots}
\usetikzlibrary{arrows}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
\begin{semilogxaxis}[
grid=both,
xmin=5e-2,                  xmax=3e1,
ymin=0,                     ymax=100,
width=12cm,                 height=8cm,
]
(22.4,100)(11.2,73.01)(1.2,39.12)(0.4,6.87)(0.08,1.53)
};
\end{semilogxaxis}
\end{tikzpicture}

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=2.5cm,y=2.5cm]
\begin{axis}[
x=2.5cm,y=2.5cm,
axis lines=middle,
grid=both,
grid style={line width=.2pt, draw=gray!30},
major grid style={line width=.4pt,draw=gray!70},
minor tick num ={3},
xmin=-3.2,
xmax=3.2,
ymin=0,
ymax=2.4,
xtick={-3.0,-2.0,...,3.0},
ytick={0.0,1.0,...,2.5},
]

\clip(-3.2,-0.2) rectangle (3.2,2.4);
\draw[samples=500,domain=-3.2:3.2]
plot(\x,{abs((\x)-2.0*floor((\x)/2.0)-1.0)+3.0/4.0*abs(4.0*(\x)-2.0*floor(4.0*(\x)/2.0)-1.0)+9.0/16.0*abs(16.0*(\x)-2.0*floor(16.0*(\x)/2.0)-1.0)});
\end{axis}
\end{tikzpicture}

\end{document}


For the first figure, I don't have a vertical line for 2*10^1.

In the second figure, I don't have a horizontal line for 2.25. (By the way, do you know how to automatically add the "0" on the axis with the option axis lines=middle ?)

On the other hand, if, for example, I write for the first figure: xmax=4e1. I do have a vertical line for 2*10^1.

Do you know what the problem is?

1. The first point is probably some numerical issue. The manual also mentions that using xtickten is faster, and it is probably more accurate. And indeed xtickten={-2,...,2}, makes the vertical grid line appear.
2. The second issue is an input problem, at least it seems a little bit unfortunate to me to use {0.0,1.0,...,2.5},, which starts with a step length of 1 but the upper bound is not consistent with that (but I am not saying it is wrong). ytick={0,1,...,3}, fixes this problem.
3. Finally, you get the 0 with the hide obscured x ticks=false key.

And an off-topic issue: you say (even twice) x=2.5cm,y=2.5cm, but this makes the plot too wide for your document. I changed this to width=\textwidth,axis equal,.

\documentclass[12pt]{book}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xmode=log,xtickten={-2,...,2},
grid=both,
xmin=5e-2,                  xmax=3e1,
ymin=0,                     ymax=100,
width=12cm,                 height=8cm,
]
(22.4,100)(11.2,73.01)(1.2,39.12)(0.4,6.87)(0.08,1.53)
};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
\begin{axis}[hide obscured x ticks=false,width=\textwidth,
axis equal,
axis lines=middle,
grid=both,
grid style={line width=.2pt, draw=gray!30},
major grid style={line width=.4pt,draw=gray!70},
minor tick num ={3},
xmin=-3.2,
xmax=3.2,
ymin=0,
ymax=2.4,
xtick={-3,-2,...,3},
ytick={0,1,...,3},
]

\clip(-3.2,-0.2) rectangle (3.2,2.4);
\draw[samples=500,domain=-3.2:3.2]
plot(\x,{abs((\x)-2.0*floor((\x)/2.0)-1.0)+3.0/4.0*abs(4.0*(\x)-2.0*floor(4.0*(\x)/2.0)-1.0)+9.0/16.0*abs(16.0*(\x)-2.0*floor(16.0*(\x)/2.0)-1.0)});
\end{axis}
\end{tikzpicture}

\end{document}


ADDENDUM: In addition to the off-topic suggestions made by Zarko for my second plot, one can improve the second plot further by choosing an appropriate number of samples that takes into account the floor function.

\documentclass[12pt]{book}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}[line cap=round,line join=round,>={Straight Barb[angle=60:2 3]}]
\begin{axis}[hide obscured x ticks=false,width=\textwidth,
axis equal image,
axis lines=middle,
grid=both,
grid style={line width=.2pt, draw=gray!30},
major grid style={line width=.4pt,draw=gray!70},
minor tick num =3,
xmin=-3.2,
xmax=3.2,
ymin=0,
ymax=2.4,
xtick={-3,-2,...,3},
ytick={-1,...,3},
]
{abs(x-2.0*floor(x/2.0)-1.0)+3.0/4.0*abs(4.0*x-2.0*floor(4.0*x/2.0)-1.0)+9.0/16.0*abs(16.0*x-2.0*floor(16.0*x/2.0)-1.0)};
\end{axis}
\end{tikzpicture}

\end{document}


But all of this is off-topic, the subquestions are answered in the upper part, i.e. my original answer.

• +1: I use pgfplots for many years now and yet I find two new options in this answer. Apr 9 '20 at 6:09

A small variation of the @Schrödinger's cat suggestion for the second diagram:

• grid is limited with xmin, xmax, ymin and ymax
• added is line for y=2.25
• used is macro \addplot
\documentclass[12pt]{book}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}[>={Straight Barb[angle=60:2 3]}]
\begin{axis}[
width=\textwidth,
axis equal image,       % <---
axis lines=middle,
axis on top,
grid=both,
grid style={thin, draw=gray!30},
major grid style={draw=gray!70},
minor tick num ={3},
xmin=-3.3,  xmax=3.3,   % <---
ymin=-0.4,  ymax=2.4,   % <---
xtick={-3,-2,...,3},
hide obscured x ticks=false,
ytick={-1,0,1,2,3},     % <---
tick label style={font=\small, inner sep=2pt, fill=white}
]