在给定的图形中画一条线

在给定的图形中画一条线
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[thick,latex-latex] (-6,0) -- (6,0)node[right]{$x$};
    \draw[thick,latex-latex] (0,-6) -- (0,6)node[above]{$y$};
    \node at (-0.3,-0.3) {O};
    \foreach \x/\l in {-5/-40,-4/-32,-3/-24,-2/-16,-1/-8,1/8,2/16,3/24,4/32,5/40}{
        \node[fill,circle,inner sep=1.5pt,label=below:$\l$] at (\x,0) {};
        \node[fill,circle,inner sep=1.5pt,label=left:$\l$] at (0,\x) {};
    }
    \draw[thick,stealth-stealth, shorten >= -2cm, shorten <= -2cm,name path =b ](0,3) -- (2,0)
    node[pos=0.65,above,sloped] {$ $};
    \foreach \x/\y/\name in {0/0/A,0/3/B}{
        \node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]above left:\name($\x, \y$)}] at (\x,\y) {};
    }
\end{tikzpicture}

\end{document}

看看我在这里所做的工作,实际上我在图中寻找一条经过两个点的线(0, 32)(-40,-40)我尝试了很多,但都没有成功。请给我一个方向,以便我可以继续前进。

答案1

你为什么不使用pgfplots

\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{intersections,arrows.meta,positioning,calc}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[xmin = -65,xmax=55,
            ymin=-65,ymax=55,
            axis x line=center,
            axis y line=center,
            xlabel={$x$},
            ylabel={$y$},
            xlabel style={below right},
            ylabel style={above left},
            axis line style={<->, {Latex}-{Latex}}]
        \coordinate (a) at (axis cs:0,32);
        \coordinate (b) at (axis cs:-40,-40);
        \draw[<->,shorten >=-3em,shorten <=-3em] (a) -- (b);
        \draw[fill=black] (a) circle (.5ex) node[pin=0:{$(0,32)$}]{};
        \draw[fill=black] (b) circle (.5ex) node[left] {$(-40,-40)$};
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

在该图中,绘制的坐标与“内部”坐标之间的比率为tikzpicture8:1。因此,当您执行 时\draw (1,0) ...,这对应于绘制的 (8,0)。因此,要到达点 (0,32),您在命令中使用的坐标\draw是 (0/8,32/8) 或 (0,4)。

你知道如何在两点之间画一条线,该线比每个点稍微延伸一点。你的图片中至少有一条这样的线。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[thick,latex-latex] (-6,0) -- (6,0)node[right]{$x$};
\draw[thick,latex-latex] (0,-6) -- (0,6)node[above]{$y$};
    \node at (-0.3,-0.3) {O};
    \foreach \x/\l in {-5/-40,-4/-32,-3/-24,-2/-16,-1/-8,1/8,2/16,3/24,4/32,5/40}{
        \node[fill,circle,inner sep=1.5pt,label=below:$\l$] at (\x,0) {};
        \node[fill,circle,inner sep=1.5pt,label=left:$\l$] at (0,\x) {};
    }
    \draw[thick,stealth-stealth, shorten >= -2cm, shorten <= -2cm,name path =b ](0,3) -- (2,0);
    \draw[thick,stealth-stealth, shorten >= -2cm, shorten <= -2cm](0,4) node[right] {$(0,32)$} -- (-5,-5) node [fill,circle,inner sep=1.5pt,label=right:{$(-40,-40)$}]{};
    node[pos=0.65,above,sloped] {$ $};
    \foreach \x/\y/\name in {0/0/A,0/3/B}{
        \node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]above right:\name($\x, \y$)}] at (\x,\y) {};
    }
\end{tikzpicture}    
\end{document}

为了更好的衡量,jak123 的答案有一个变体:

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}

\pgfplotsset{
    myaxis/.style={axis line style={Latex-Latex}}
}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xmin = -65,xmax=55,
            ymin=-65,ymax=55,
            axis x line=center,
            axis y line=center,
            xlabel={$x$},
            ylabel={$y$},
            xlabel style={below right},
            ylabel style={above left},
            myaxis,
            clip mode=individual]
    \addplot [Latex-Latex,domain=-50:10,samples=2] {1.8*x + 32};
    \node[circle,inner sep=0pt,minimum size=4pt,fill,label=left:{$(-40,-40)$}] at (axis cs:-40,-40) {};
    \node[circle,inner sep=0pt,minimum size=4pt,fill,label=right:{$(0,32)$}] at (axis cs:0,32) {};
    \end{axis}
\end{tikzpicture}
\end{document}

相关内容