13

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?

1

3 Answers 3

11

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

19

As Claudio mentioned in his comment, you could use PGFPlots for this:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmode=log, ymode=log,
    xmin=1e-1, xmax=1e4,
    ymin=1e-1, ymax=1e4,
    grid=both,
    major grid style={black!50}
]
\end{axis}
\end{tikzpicture}
\end{document}

Or with only one logarithmic axis:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmode=log,
    xmin=1e-1, xmax=1e4,
    ymin=1e-1, ymax=1e4,
    grid=both,
    major grid style={black!50}
]
\end{axis}
\end{tikzpicture}
\end{document}
0

In addition to the basic {axis} environment, pgfplots also has built in {semilogxaxis}, {semilogyaxis} and {loglogaxis} environments already included. These behave exactly the same way as specifying the axis modes in the options, but results in less verbose code. See the pgfplots manual the pgfplots manual for details and examples.

2
  • 1
    Welcome to TeX.SX. I don't see any new information compared to Jake's answer. Have I missed something? Commented Sep 26, 2016 at 15:09
  • Thank you! There is no difference in functionality, just that there is an alternative method to setting the axis modes in the options block. Commented Sep 27, 2016 at 2:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .