使用 pgfplots,我想要类似以下的网格线GeoGebra 的。这是在主刻度线之间有四个次要刻度线,并且次要网格线的颜色较浅:
在...的帮助下这个帖子,我设法得到了这个代码
\documentclass[margin=0.5cm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
grid=both,
xtick distance=1,
ytick distance=1,
xmin=-3.5,
xmax=3.5,
ymin=-3.5,
ymax=3.5,
x=1cm,
y=1cm,
xlabel={$x$},
ylabel={$y$},
xlabel style={right},
ylabel style={above},
minor tick num=4,
every minor tick/.style={minor tick length=0pt}
]
\end{axis}
\end{tikzpicture}
\end{document}
其结果如下:
但是,这看起来仍然不太像 GeoGebra 的网格线。有没有办法让主网格线和次网格线有单独的颜色?
提前感谢您的帮助!
答案1
在文档第 372 页中major grid style={gray}, minor grid style={gray!50}
\documentclass[margin=0.5cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
grid=both,
xtick distance=1,
ytick distance=1,
xmin=-3.5,
xmax=3.5,
ymin=-3.5,
ymax=3.5,
x=1cm,
y=1cm,
xlabel={$x$},
ylabel={$y$},
xlabel style={right},
ylabel style={above},
minor tick num=4,
every minor tick/.style={minor tick length=0pt},
%%%
major grid style={gray}, minor grid style={gray!50}%<-- added
]
\end{axis}
\end{tikzpicture}
\end{document}
另一种选择是使用tkz-base
\documentclass[margin=0.5cm]{standalone}
\usepackage{tkz-base}
\begin{document}
\def\tkzCoeffSubColor{20}% instead of 50
\def\tkzCoeffSubLw{0.2}% instead of 0.75
\begin{tikzpicture}
\tkzInit[
xmin=-3.5,
xmax=3.5,
ymin=-3.5,
ymax=3.5,
]
% we can change the step for the second grid
\tkzGrid[sub,color=orange,
subxstep=.2,subystep=.2]
\tkzAxeXY
\end{tikzpicture}
\end{document}