如何使用 pgfplots 生成上述图?
我这里有一个示例代码:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,axis equal,grid=both]
\addplot coordinates{(-3,1) (6,-2)};
\end{axis}
\end{tikzpicture}
\end{document}
这将生成如下所示的图形:
我不希望网格线画满。
仅需要较小的 xticks 和 yticks。
不希望将点放置在线的末端。
答案1
可以通过以下方式轻松完成pstricks
:
\documentclass[svgnames, pstricks, border=10pt]{standalone}
\usepackage{pst-plot, pst-node}
\begin{document}
\begin{pspicture*}(-0.5,-1.5)(6,6.5)
\psset{showorigin=false, arrowinset=0.12, linejoin=1, labelsep=1pt}
\everypsbox{\scriptsize}
\psaxes[labelFontSize=\scriptstyle, tickstyle=full, ticksize=2pt -2pt, Dx=1, labels=none, linecolor=LightSteelBlue]{->}(0,0)(-0.5,-1.5)(5.5, 6)[$x$,0][$y$,90]
\pnodes(-0.5,-1.5){A} (5,4){B}(-0.5,3.5){C}(4,-1){D}
\ncline[linecolor=Tomato]{A}{B}\naput[nrot=:U, npos=0.8]{$ x-y=1 $}
\ncline[linecolor=LawnGreen]{C}{D}\naput[nrot=:U, npos=0.3]{$ x+y=3 $}
\dotnode(2,1){I}\uput{4pt}[r](I){$P(2,1)$}
\end{pspicture*}
\end{document}
答案2
看看以下内容是否符合您的要求:
\documentclass[border=3.141592]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
lbl/.style = {text=black, anchor=south, sloped},
dot/.style = {circle, fill, inner sep=1.6pt,
node contents={}}
]
\begin{axis}[
axis lines=middle,
axis equal,
xlabel=$x$, x label style={anchor=west},
ylabel=$y$, y label style={anchor=south},
grid=both,
ytick={-5,-4,...,9},
ticklabel style={font=\footnotesize, fill=white,
inner sep=2pt},
ymin=-1.5, ymax=4.5,
no marks,
every axis plot post/.append style={green, thick}
]
\addplot coordinates{(-1, 4) (5,-2)}
node[near start, lbl] {$x+y=3$};
\addplot coordinates{(-1,-2) (5, 4)}
node[near end, lbl] {$x-y=1$};
\node at (2,1) [dot,label=right:${P(2,1)}$];
\end{axis}
\end{tikzpicture}
\end{document}
编辑(1): 图表线条为绿色且较粗。
编辑(2): 直线的交点在 $P(2,1)$ 处,而不是在 $P(2.1)$ 处。
答案3
使用渐近线:
// http://asymptote.ualberta.ca/
unitsize(1cm);
import graph;
real f(real x){return 3-x;}
real g(real x){return x-1;}
path pf=graph(f,-1,4.5);
path pg=graph(g,-1,5);
pair P=intersectionpoint(pf,pg);
draw((P.x,0)--P^^(0,P.y)--P,gray+dashed);
draw(Label("$x+y=3$",align=S,EndPoint),pf,blue+1pt);
draw(Label("$x-y=1$",align=N,EndPoint),pg,orange+1pt);
dot("$P=(1,2)$",align=2E,P,red);
label("O",align=SW,(0,0));
xaxis(Label("$x$",align=N),Ticks(begin=false,beginlabel=false,Step=1,step=0,Size=3,OmitTick(0)),Arrow(TeXHead));
yaxis(Label("$y$",align=E),Ticks(begin=false,beginlabel=false,Step=1,step=0,Size=3,OmitTick(0)),Arrow(TeXHead));
shipout(bbox(5mm,invisible));
答案4
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
axis equal,
xticklabels=none, yticklabels=none,
]
\addplot[green, no marks] coordinates{(-3,1) (6,-2)};
\end{axis}
\end{tikzpicture}
\end{document}