[Tex/LaTex] Easy creation of logarithmic grid with TikZ

pgfplotsplottikz-pgf

Is it possible to create a logarithmic grid using the command below with TikZ?

\draw (-0.1,-0.1) grid (4.1,4.1);

It would be great to be able to create a logarithmic grid without having to specify each line manually. If it is possible, can you also create log-grid on both and only one axis?

Best Answer

you can adapt the \semilog function of theBodegraph package http://sciences-indus-cpge.papanicola.info/Bode-Black-et-Nyquist-avec-Tikz

for the semilog grid

\newcommand{\semilog}[5][]{
\pgfmathparse{int(#3-1)}\let\Xmax\pgfmathresult
\foreach \ee in{#2,...,\Xmax}{
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin] ({log10(\x)+\ee},#4) -- ({log10(\x)+\ee},#5);}
\draw[thin, red] (\ee,#4)node[below]{$10^{\ee}$} -- ({\ee},#5);
};
\draw[thin, red] ({#3},#4)node[name=TextX,below]{$10^{#3}$} -- ({#3},#5);
\pgfmathparse{int(#4+\valpas)}
\let\Valsuivante\pgfmathresult 
\foreach \yy in  {#4,\Valsuivante,...,#5}{
\draw[thin, red] (#2,\yy) node[left,name=TextY]{$\yy$} -- ({#3},\yy);};

\node[  above of= TextY,node distance=0.6em,above] { \Unity};
\node[ right]at (#3,#4){ \Unitx};
}

enter image description here

the dimension of the grid are specified in the scope, choosing the scale along x and along y

for the loglog grid

\newcommand{\loglog}[5][]{
\pgfmathparse{int(#3-1)}\let\Xmax\pgfmathresult
\foreach \ee in{#2,...,\Xmax}{
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin] ({log10(\x)+\ee},#4) -- ({log10(\x)+\ee},#5);}
\draw[thin, red] (\ee,#4)node[below]{$10^{\ee}$} -- ({\ee},#5);
};
\draw[thin, red] ({#3},#4)node[name=TextX,below]{$10^{#3}$} -- ({#3},#5);
\pgfmathparse{int(#4+\valpas)}
\pgfmathparse{int(#5-1)}\let\Ymax\pgfmathresult
\let\Valsuivante\pgfmathresult 
\foreach \yy in  {#4,...,\Ymax}{
\draw[thin, red] (#2,\yy) node[left,name=TextY]{$10^\yy$} -- ({#3},\yy);
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin,blue] (#2,{log10(\x)+\yy}) -- (#3,{log10(\x)+\yy});
}
\draw[thin, red] ({#2},\yy)node[name=TextY,left]{$10^{\yy}$} -- ({#3},\yy);
}
\draw[thin, red] ({#2},#5)node[name=TextY,left]{$10^{#3}$} -- ({#3},#5);
\node[  above of= TextY,node distance=0.6em,right] { \Unity};
\node[ right]at (#3,#4){ \Unitx};
}

enter image description here

the same without axes

\newcommand{\loglogN}[3][]{
\pgfmathparse{int(#2-1)}\let\Xmax\pgfmathresult
\foreach \ee in{0,...,\Xmax}{
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin,black] ({log10(\x)+\ee},0) -- ({log10(\x)+\ee},#3);}
\draw[thin, red] (\ee,0)-- ({\ee},#3);
};
\draw[thin, red] ({#2},0) -- ({#2},#3);
\pgfmathparse{int(0+\valpas)}
\pgfmathparse{int(#3-1)}\let\Ymax\pgfmathresult
\let\Valsuivante\pgfmathresult 
\foreach \yy in  {0,...,\Ymax}{
\draw[thick, red] (0,\yy)  -- ({#2},\yy);
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin,blue] (0,{log10(\x)+\yy}) -- (#2,{log10(\x)+\yy});
}
}
\draw[thin, red] ({0},#3)-- ({#2},#3);
}

enter image description here

the complete MWE

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,fit,intersections,shapes,calc}


\def\valpi{3.1415957}
\def\valpas{10}
\def\Unitx{rd/s}
\def\Unity{dB}

\newcommand{\semilog}[5][]{
\pgfmathparse{int(#3-1)}\let\Xmax\pgfmathresult
\foreach \ee in{#2,...,\Xmax}{
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin] ({log10(\x)+\ee},#4) -- ({log10(\x)+\ee},#5);}
\draw[thin, red] (\ee,#4)node[below]{$10^{\ee}$} -- ({\ee},#5);
};
\draw[thin, red] ({#3},#4)node[name=TextX,below]{$10^{#3}$} -- ({#3},#5);
\pgfmathparse{int(#4+\valpas)}
\let\Valsuivante\pgfmathresult 
\foreach \yy in  {#4,\Valsuivante,...,#5}{
\draw[thin, red] (#2,\yy) node[left,name=TextY]{$\yy$} -- ({#3},\yy);};

\node[  above of= TextY,node distance=0.6em,above] { \Unity};
\node[ right]at (#3,#4){ \Unitx};
}


\newcommand{\loglog}[5][]{
\pgfmathparse{int(#3-1)}\let\Xmax\pgfmathresult
\foreach \ee in{#2,...,\Xmax}{
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin] ({log10(\x)+\ee},#4) -- ({log10(\x)+\ee},#5);}
\draw[thin, red] (\ee,#4)node[below]{$10^{\ee}$} -- ({\ee},#5);
};
\draw[thin, red] ({#3},#4)node[name=TextX,below]{$10^{#3}$} -- ({#3},#5);
\pgfmathparse{int(#4+\valpas)}
\pgfmathparse{int(#5-1)}\let\Ymax\pgfmathresult
\let\Valsuivante\pgfmathresult 
\foreach \yy in  {#4,...,\Ymax}{
\draw[thin, red] (#2,\yy) node[left,name=TextY]{$10^\yy$} -- ({#3},\yy);
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin,blue] (#2,{log10(\x)+\yy}) -- (#3,{log10(\x)+\yy});
}
}
\draw[thin, red] ({#2},#5)node[name=TextY,left]{$10^{#3}$} -- ({#3},#5);
\node[  above of= TextY,node distance=0.6em,right] { \Unity};
\node[ right]at (#3,#4){ \Unitx};
}

\newcommand{\loglogN}[3][]{
\pgfmathparse{int(#2-1)}\let\Xmax\pgfmathresult
\foreach \ee in{0,...,\Xmax}{
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin,black] ({log10(\x)+\ee},0) -- ({log10(\x)+\ee},#3);}
\draw[thin, red] (\ee,0)-- ({\ee},#3);
};
\draw[thin, red] ({#2},0) -- ({#2},#3);
\pgfmathparse{int(0+\valpas)}
\pgfmathparse{int(#3-1)}\let\Ymax\pgfmathresult
\let\Valsuivante\pgfmathresult 
\foreach \yy in  {0,...,\Ymax}{
\draw[thick, red] (0,\yy)  -- ({#2},\yy);
    \foreach \x in {1,2,3,4,5,6,7,8,9}{
\draw[thin,blue] (0,{log10(\x)+\yy}) -- (#2,{log10(\x)+\yy});
}
}
\draw[thin, red] ({0},#3)-- ({#2},#3);
}

\begin{document}

\begin{tikzpicture}

\begin{scope}[xscale=15/4,yscale=5/60]
\semilog{-1}{3}{-20}{40}
\end{scope}

\def\Unitx{}
\def\Unity{}

\begin{scope}[shift={(0,-15)},xscale=15/4,yscale=6/5]
\loglog{-1}{3}{-2}{3}
\end{scope}

\begin{scope}[shift={(0,-10)},xscale=15/4,yscale=6/5]
\loglogN{4}{5}
\end{scope}

\end{tikzpicture}

\end{document}

this code is certainly optimizable