tikz 中绘制的线停止在网格中间

tikz 中绘制的线停止在网格中间

绘制的线停止并且不会延伸到网格的边界: 在此处输入图片描述

这是我的代码:

\documentclass{article}

\usepackage{tikz}
\usepackage{amsmath}

\begin{document}

\begin{tikzpicture}[baseline={(current bounding box.north)}]
  \draw[step=1cm,color=gray!20] (-3,-2) grid (6,6);
  \draw[-] (-3,0) -- (6,0) node[right] {$x$};
  \draw (0,0) node[below left] {$O$};
  \draw[-] (0,-2) -- (0,6) node[above] {$y$};
  \foreach \x in {-3, -2, -1, 1, 2, 3, 4, 5, 6}
    \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
  \foreach \y in {-2, -1, 1, 2, 3, 4, 5, 6}
    \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$}; 
  \draw[blue] (-2,4) node[below left] {$k$};
  \draw[black] (4,5) node[above left] {$A$}; 
  \clip (-3,-2) rectangle (6,6);
  \draw[scale=1,smooth,variable=\x,blue] plot ({\x},{(-4/3)*\x});
  \draw[scale=1,smooth,variable=\x,red] plot ({\x},{(3/4)*\x+2});
  \draw[fill=black](4,5) circle(0.5mm);
\end{tikzpicture}

\end{document}

我该怎么做才能让线位于网格的边界上?

答案1

使用domain=-4:7;

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[baseline={(current bounding box.north)}]
  \draw[step=1cm,color=gray!20] (-3,-2) grid (6,6);
  \draw[-] (-3,0) -- (6,0) node[right] {$x$};
  \draw (0,0) node[below left] {$O$};
  \draw[-] (0,-2) -- (0,6) node[above] {$y$};
  \foreach \x in {-3, -2, -1, 1, 2, 3, 4, 5, 6}
    \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
  \foreach \y in {-2, -1, 1, 2, 3, 4, 5, 6}
    \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$}; 
  \draw[blue] (-2,4) node[below left] {$k$};
  \draw[black] (4,5) node[above left] {$A$}; 
    \clip (-3,-2) rectangle (6,6);
  \draw[scale=1,smooth,variable=\x,blue] plot ({\x},{(-4/3)*\x});
  \draw[scale=1,smooth,variable=\x,red,domain=-4:6] plot ({\x},{(3/4)*\x+2});
  \draw[fill=black](4,5) circle(0.5mm);
\end{tikzpicture}
\end{document}

答案2

使用以下方式可以更好地绘制图表pgfplots

在此处输入图片描述

笔记:

  • axis cs:当要使用 之前的版本生成时才需要1.11。因此,添加\pgfplotsset{compat=1.11}并使用最新版本的 pgfplots 时,您不需要axis cs:

代码:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}

%% http://tex.stackexchange.com/questions/17438/how-to-properly-scale-a-tikz-pgf-picture-which-has-a-beginaxis-endaxis
\pgfkeys{/pgfplots/Axis Labels At Tip/.style={
        xlabel style={
            at={(current axis.right of origin)}, 
            xshift=1.5ex, anchor=center
        },
        ylabel style={
            at={(current axis.above origin)}, 
            yshift=1.5ex, anchor=center
        }
    }
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
        x=1cm, y=1cm,% Better to let pgfplots do this, added in case it is important
        axis y line=center,
        axis x line=center, 
        xmin=-3, xmax=6,
        ymin=-2, ymax=6,
        grid=major,
        Axis Labels At Tip,
        xlabel=$x$, ylabel=$y$,
    ]
    \addplot [blue, thick, domain=-3:6] {-(4/3)*x};
    \addplot [red,  thick, domain=-3:6] {(3/4)*x + 2};

    \node [below left, blue] at (axis cs: -2,4) {$k$};
    \node [below left] at (axis cs: 0,0) {$O$};
    \draw[fill=black]  (axis cs: 4,5) circle(0.5mm)
        node[above left] at (axis cs: 4,5) {$A$};
\end{axis}
\end{tikzpicture}
\end{document}

代码:\pgfplotsset{compat=1.11}

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}

\pgfplotsset{compat=1.11}

%% http://tex.stackexchange.com/questions/17438/how-to-properly-scale-a-tikz-pgf-picture-which-has-a-beginaxis-endaxis
\pgfkeys{/pgfplots/Axis Labels At Tip/.style={
        xlabel style={
            at={(current axis.right of origin)}, 
            xshift=1.5ex, anchor=center
        },
        ylabel style={
            at={(current axis.above origin)}, 
            yshift=1.5ex, anchor=center
        }
    }
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
        x=1cm, y=1cm,% Better to let pgfplots do this, added in case it is important
        axis y line=center,
        axis x line=center, 
        xmin=-3, xmax=6,
        ymin=-2, ymax=6,
        grid=major,
        Axis Labels At Tip,
        xlabel=$x$, ylabel=$y$,
    ]
    \addplot [blue, thick, domain=-3:6] {-(4/3)*x};
    \addplot [red,  thick, domain=-3:6] {(3/4)*x + 2};

    \node [below left, blue] at (-2,4) {$k$};
    \node [below left] at (0,0) {$O$};
    \draw[fill=black]  (4,5) circle(0.5mm)
        node[above left] at (4,5) {$A$};
\end{axis}
\end{tikzpicture}
\end{document}

答案3

PSTricks 解决方案使用pst-plot包裹:

\documentclass{article}

\usepackage{pst-plot}

\begin{document}

\begin{pspicture}(-3.2,-2.1)(6.65,6.7)
  \psaxes[
    labels = none,
    tickcolor = black!30,
    tickwidth = 0.5pt,
    xticksize = -2 6,
    yticksize = -3 6
  ]{->}(0,0)(-3,-2)(6.3,6.3)[$x$,0][$y$,90]
  \psaxes{->}(0,0)(-3,-2)(6.3,6.3)[$x$,0][$y$,90]
  \uput[225](0,0){$O$}
 \psset{algebraic}
  \psplot[
    linecolor = blue
  ]{-3}{1.5}{-4/3*x}
  \rput(-2.37,3.63){$\color{blue}k$}
  \psplot[
    linecolor = red
  ]{-3}{5.333}{3/4*x+2}
  \psdot(4,5)
  \uput[126.87](4,5){$A$}
\end{pspicture}

\end{document}

输出

相关内容