A(1,1), B(2,2), C(1,3)
尝试使用以下代码绘制由顶点三角形所包围的区域:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}[h]
\begin{tikzpicture}
\begin{axis}
[xlabel=$x$, ylabel=$y$, axis lines=middle, xtick={6}, ytick={6}, no marks, axis equal, xmin=-1, xmax=4, ymin=-1, ymax=4, enlargelimits={upper=0.1}]
\addplot[no markers,samples=51, thick, domain=1:3, variable=t]({1},{t});
\addplot[no markers,samples=51, thick, domain=1:2, variable=t, name path=A]({t},{t});
\addplot[no markers,samples=51, thick, domain=1:2, variable=t, name path=B]({t},{4-t});
\addplot[gray!30,opacity=0.6] fill between[of=A and B, soft clip={domain=1:2}];
\path node[below left] at (0,0) {$O$};
\draw node[below left] at (1,0) {$1$};
\draw node[below] at (2,0) {$2$};
\draw node[left] at (0,1) {$1$};
\draw node[left] at (0,2) {$2$};
\draw node[left] at (0,3) {$3$};
\draw node[below left] at (1,1) {$A$};
\draw node[right] at (2,2) {$B$};
\draw node[above] at (1,3) {$C$};
\draw node[below right] at (1.5,2) {$D$};
\draw node[right] at (1,-.8) {\footnotesize{$x=1$}};
\draw[densely dotted,black] (2,2) -- (3,3);
\draw[densely dotted,black] (2,2) -- (3,1);
\draw node[right] at (3,3) {\footnotesize{$y=x$}};
\draw node[right] at (3,1) {\footnotesize{$y=4-x$}};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
因此没有指出任何节点。我错在哪里?
答案1
与上一个问题不同,你遗漏了
\pgfplotsset{compat=1.15}
使用compat=1.11
或更新版本,TikZ 命令(\draw
,,)中的坐标被解释为轴坐标,但如果没有设置,或者设置或更低,\path
则需要使用而不是如果您希望坐标获得相同。\node
compat
1.10
(axis cs:0,0)
(0,0)
在这种情况下,大多数节点都不需要,让我们pgfplots
为 x 和 y 刻度制作标签。我认为将node
s 附加到\addplot
s 会更方便标签的顶点和虚线的函数标签。
其他一些小问题:为什么不使用\node
而不是\draw node
? 并且\footnotesize
不是一个接受参数的宏,它是一个影响同一组/环境中的以下文本的字体开关。因此,您将其用作\footnotesize text
,而不是\footnotesize{text}
,以限制其效果,使用{\footnotesize text}
。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$, ylabel=$y$,
axis lines=middle,
axis equal,
xmin=-1, xmax=4,
ymin=-1, ymax=4,
enlargelimits={upper=0.1},
% set the specific ticks you need
xtick={2},
ytick={1,2,3},
no markers,
% all straight lines, 2 samples is all you need
samples=2,
% sets the length of the ticks, i.e. the small lines
tickwidth=0pt
]
\addplot[thick, domain=1:3, variable=t] ({1},{t})
node[below,pos=0] {$A$}
node[above,pos=1] {$C$};
\addplot[thick, domain=1:2, variable=t, name path=A]({t},{t})
node[right] {$B$};
\addplot[thick, domain=1:2, variable=t, name path=B]({t},{4-t});
\addplot[gray!30,opacity=0.6] fill between[of=A and B, soft clip={domain=1:2}];
\addplot[densely dotted, domain=2:3, variable=t]({t},{t})
node[right,font=\footnotesize] {$y=x$};
\addplot[densely dotted, domain=2:3, variable=t]({t},{4-t})
node[right,font=\footnotesize] {$y=4-x$};
\node[below left] at (0,0) {$O$};
\node[below left] at (1,0) {$1$};
\node[below left] at (1.5,2) {$D$};
\draw [densely dotted] (0,2) -| (2,0);
\draw [densely dotted] (0,3) -- (1,3);
\draw [densely dotted] (0,1) -| (1,-0.5)
node[right,font=\footnotesize] {$x=1 $};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
添加\pgfplotsset{compat=1.15}
代码
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{figure}[h]
\begin{tikzpicture}
\begin{axis}
[xlabel=$x$, ylabel=$y$, axis lines=middle, xtick={6}, ytick={6}, no marks, axis equal, xmin=-1, xmax=4, ymin=-1, ymax=4, enlargelimits={upper=0.1}]
\addplot[no markers,samples=51, thick, domain=1:3, variable=t]({1},{t});
\addplot[no markers,samples=51, thick, domain=1:2, variable=t, name path=A]({t},{t});
\addplot[no markers,samples=51, thick, domain=1:2, variable=t, name path=B]({t},{4-t});
\addplot[gray!30,opacity=0.6] fill between[of=A and B, soft clip={domain=1:2}];
\path node[below left] at (0,0) {$O$};
\draw node[below left] at (1,0) {$1$};
\draw node[below] at (2,0) {$2$};
\draw node[left] at (0,1) {$1$};
\draw node[left] at (0,2) {$2$};
\draw node[left] at (0,3) {$3$};
\draw node[below left] at (1,1) {$A$};
\draw node[right] at (2,2) {$B$};
\draw node[above] at (1,3) {$C$};
\draw node[below right] at (1,2) {$D$};
\draw node[right] at (1,-.8) {\footnotesize{$x=1$}};
\draw[densely dotted,black] (2,2) -- (3,3);
\draw[densely dotted,black] (2,2) -- (3,1);
\draw[densely dotted,black] (1,1) -- (1,-1);
\draw[densely dotted,black] (2,2) -- (2,0);
\draw[densely dotted,black] (0,3) -- (1,3);
\draw[densely dotted,black] (0,2) -- (2,2);
\draw[densely dotted,black] (0,1) -- (1,1);
\draw node[right] at (3,3) {\footnotesize{$y=x$}};
\draw node[right] at (3,1) {\footnotesize{$y=4-x$}};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}