以下是我编写的代码:
\usepackage{tikz,pgfplots,amsmath}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1]
\begin{scope}[local bounding box=grid]
\draw[cyan!25,ultra thick] (-2,-2) grid (7,6);
\draw[step=5mm,cyan!25,thick] (-2,-2) grid (7,6);
\draw[step=1mm,cyan!25,very thin] (-2,-2) grid (7,6);
\draw[-latex,thick] (-2,0)--(7.4,0) node[right] {\footnotesize $(1)$};
\draw[-latex,thick] (0,-2)--(0,6.4) node[above] {\footnotesize $(2)$};
\draw[-,thick] (-0.2,1) node[anchor=east]{1} --(0.2,1);
\draw[-,thick] (1,-0.2) node[anchor=north]{1} --(1,0.2);
\end{scope}
\begin{scope}[shift={(grid.center)}]
\begin{axis}[
%grid = major,
axis lines=none,
ticks=none
]
\addplot [domain=-1:5,smooth, ultra thick, black!20!red] {0.5*x^2-3*x+2.5};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}
图表的位置不对。第二个范围的“轴”应与第一个范围的“轴”重合,即其坐标系中的点 (0,0) 应相同。不知何故,我不知道该怎么做,因为我仍在学习 tikz。
顺便说一句:如果有更简单的解决方案,您不需要覆盖两个东西,请告诉我。
答案1
绘制所有需要相对于axis
环境缩放或对齐的内容要容易得多里面这个环境:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=none,
ticks=none,
x=1cm,
y=1cm,
clip=false
]
\draw[cyan!25, ultra thick] (-2,-2) grid (7,6);
\draw[step=5mm, cyan!25, thick] (-2,-2) grid (7,6);
\draw[step=1mm, cyan!25, very thin] (-2,-2) grid (7,6);
\draw[-latex, thick] (-2,0) -- (7.4,0) node[right] {\footnotesize $(1)$};
\draw[-latex, thick] (0,-2) -- (0,6.4) node[above] {\footnotesize $(2)$};
\draw[thick] (-0.2,1) node[anchor=east] {1} -- (0.2,1);
\draw[thick] (1,-0.2) node[anchor=north] {1} -- (1,0.2);
\addplot[domain=-1:5, smooth, ultra thick, black!20!red] {0.5*x^2-3*x+2.5};
\end{axis}
\end{tikzpicture}
\end{document}