如图所示,我从第一个交点(y=2x 和 y=6-x)到 x(2) 添加了一条线。但我无法从第二个交点(y=x 和 y=6-x)到 x(3) 添加一条线。我试过了,intersection-2
但出现了错误。我想使用交集命令来实现这一点因为可能会出现我无法准确找到交点的新情况。
我应该怎么办?
\documentclass[11pt,a4paper]{article}%
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{tikz,pgf,pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns,intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
height=7cm, width=7cm,
xlabel=$x$,
ylabel=$y$,
enlargelimits,
ytick=\empty,
xtick={2,3},
every axis y label/.style={at=(current axis.above origin),anchor=south},
every axis x label/.style={at=(current axis.right of origin),anchor=west}],
\addplot[name path=f,blue,domain={0:2}] {2*x} node[pos=.8, above]{};
\addplot[name path=h,blue,domain={2:3}] {6-x} node[pos=.8, above,,rotate=-15]{};
\addplot[name path=g,blue,domain={0:3}] {x} node[pos=.8, below,rotate=30]{};
\addplot[name path=bos,white,domain={2:4}] {x/2} node[pos=.8, below,rotate=30]{};
\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=f and g, soft clip={domain=0:2}];
\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=h and g, soft clip={domain=2:3}];
\node[coordinate,pin=95:{\footnotesize{$y=2x$}}] at (axis cs:1,2){};
\node[coordinate,pin=-45:\footnotesize{$y=x$}] at (axis cs:0.8,0.8){};
\node[coordinate,pin=40:\footnotesize{$y=6-x$}] at (axis cs:2.5,3.5){};
\draw [name intersections={of=f and h},ultra thin,gray] ({axis cs:0,0}-|intersection-1) -- (intersection-1);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您需要独立计算交点才能在同一路径中使用它们。否则,您可以name intersections
在具有不同路径名的每条路径上使用。
一种选择是使用不同的名称前缀。
编辑关于问题;
- 您在线条的端点处相交,这会导致数值不稳定。您命名的冗余路径
boş
会导致绘制的图形变小,从而缩小路径。相反,您可以稍微增加线条的范围以确保正确相交。 - 为了延长轴,你可以使用
xmax
键,就像我下面做的那样。或者,clip=false
如果你需要打印该节点,也可以使用 - 如果您不想绘制任何东西,请
white
使用draw=none
。
\documentclass[11pt,a4paper]{article}%
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{tikz,pgf,pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns,intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
height=7cm, width=7cm,
xlabel=$x$,
ylabel=$y$,
enlargelimits,
ytick=\empty,
xtick={2,3},xmax=4,% <---Added xmax instead
every axis y label/.style={at=(current axis.above origin),anchor=south},
every axis x label/.style={at=(current axis.right of origin),anchor=west}],
\addplot[name path=f,blue,domain={0:2}] {2*x} node[pos=.8, above]{};
\addplot[name path=h,blue,domain={2:3.005}] {6-x} node[pos=.8, above,,rotate=-15]{};
\addplot[name path=g,blue,domain={0:3.005}] {x} node[pos=.8, below,rotate=30]{};
%\addplot[name path=bos,draw=none,domain={2:4}] {x/2} node[pos=.8, below,rotate=30]{};
\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=f and g, soft clip={domain=0:2}];
\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=h and g, soft clip={domain=2:3}];
\node[coordinate,pin=95:{\footnotesize{$y=2x$}}] at (axis cs:1,2){};
\node[coordinate,pin=-45:\footnotesize{$y=x$}] at (axis cs:0.8,0.8){};
\node[coordinate,pin=40:\footnotesize{$y=6-x$}] at (axis cs:2.5,3.5){};
\draw [name intersections={of=f and h,name=fh},
name intersections={of=h and g,name=hg},
ultra thin,gray]
(hg-1) -- (axis cs:3,0)
(fh-1) -- (axis cs:2,0)
;
\end{axis}
\end{tikzpicture}
\end{document}