答案1
与您开始绘制的内容类似pgfplots
:
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
grid=both,
grid,
ymin=-5,
ymax=5,
xmin=-5,
xmax=5,
xtick = {-5,...,5},
ytick = {-5,...,5},
]
\draw[stealth-stealth,very thick] (axis cs:-4,-4) -- (axis cs:3,4) node[below right] at (axis cs:0,0) {A};
\draw[stealth-stealth,very thick] (axis cs:-2,-3) -- (axis cs:-2,4) node[left] at (axis cs:-2,1) {B};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
这只是略微改进的版本罗兰的回答。
% used PGFPlots v1.18.1
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% use this `compat` level or higher to place TikZ coordinates by default
% in axis coordinates. That means TikZ coordinates don't have to be given
% the coordinate system `axis cs:` explicitly.
% It is best practice to use the actual version, i.e. here it would be 1.18.
% That ensures that you use the most recent features.
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}[
% create a style for the lines that can later be used
my line style/.style={
>=stealth,
<->,
very thick,
}
]
\begin{axis}[
axis lines=middle,
grid=both,
grid,
ymin=-5,
ymax=5,
xmin=-5,
xmax=5,
xtick distance=1,
ytick distance=1,
]
\draw [my line style] (-4,-4) -- ( 3,4)
% to place a node long the path either give everything explicitly ...
node [below right,red] at (0,0) {A};
\draw [my line style] (-2,-3) -- (-2,4)
% ... or a bit simplified.
node [auto,pos=0.6] {B};
\end{axis}
\end{tikzpicture}
\end{document}