Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
Why not using pgfplots? –  Claudio Fiandrino Feb 19 '13 at 16:38

2 Answers 2

up vote 6 down vote accepted

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

share|improve this answer

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.