我绘制了以下图表:
\begin{figure}[htpb]
\centering
\begin{tikzpicture}
\begin{axis}[
domain=-3:5,
smooth,
axis x line=middle,
axis y line=middle,
xlabel=\(x\),
ylabel=\(y\),
grid=both,
xtick={-1, 1},
ytick={2, 3},
ymin=-5,
ymax=4,
]
\addplot[black, thick] {-x^2 + 2 * x + 2};
\addplot[red, thick, dashed] coordinates{(1,10) (1,-25)};
\end{axis}
\end{tikzpicture}
\end{figure}
如何保留主/次网格线,同时仍指定我想要的刻度?
答案1
一种方法是删除默认刻度xticklabels={}
并添加您自己的extra x ticks={-1,0.5,1}
(对于 y 轴)。通常,额外的刻度会自带网格线。要禁用此功能,您可以使用extra tick style={grid=none}
。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-3:5,
smooth,
axis x line=middle,
axis y line=middle,
xlabel=\(x\),
ylabel=\(y\),
grid=both,
minor tick num=1,
minor grid style={blue!10!white},
extra tick style={grid=none},
xticklabels={},
extra x ticks={-1,0.5,1},
yticklabels={},
extra y ticks={2, 3},
ymin=-5,
ymax=4,
]
\addplot[black, thick] {-x^2 + 2 * x + 2};
\addplot[red, thick, dashed] coordinates{(1,10) (1,-25)};
\end{axis}
\end{tikzpicture}
\end{document}