是否可以使用以下命令使用 TikZ 创建对数网格?
\draw (-0.1,-0.1) grid (4.1,4.1);
如果能够创建对数网格而无需手动指定每条线,那就太好了。如果可以的话,您也可以在两个轴上或只在一个轴上创建对数网格吗?
答案1
您可以调整Bodegraph 包中的 \semilog 函数 http://sciences-indus-cpge.papanicola.info/Bode-Black-et-Nyquist-avec-Tikz
对于半对数网格
\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};
}
网格的尺寸在范围内指定,选择沿 x 和 y 的比例
对于对数对数网格
\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};
}
没有轴也一样
\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);
}
完整的 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}
这段代码肯定是可以优化的
答案2
正如 Claudio 在他的评论中提到的,你可以使用 PGFPlots 来实现这一点:
\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}
或者只有一个对数轴:
\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}
答案3
除了基本{axis}
环境之外,pgfplots 还内置了{semilogxaxis}
和{semilogyaxis}
环境{loglogaxis}
。这些行为与在选项中指定轴模式完全相同,但代码更简洁。请参阅 pgfplots 手册pgfplots 手册了解详细信息和示例。