在约束之间绘制可行区域(TikZ 和 pgf)

在约束之间绘制可行区域(TikZ 和 pgf)

我试图对可行域 ABCDE 进行着色,但出于某种原因,只得到一条奇怪的线。这是代码:

\begin{tikzpicture}
% axes
\begin{axis}[axis on top,smooth,
axis line style=very thick,
axis x line=bottom,
axis y line=left,
ymin=0,ymax=17,xmin=0,xmax=17,
xlabel=$x_1$, ylabel=$x_2$,grid=major
]
% constraints
\draw [name path global=xaxis] (0,0) -- (17,0);
\draw [name path=yaxis] (0,0) -- (0,17);
\addplot[name path global=firstline,very thick, domain=0:17]{6-0.5*x};
\addplot[name path global=secondline,very thick, domain=0:17]{3+0.5*x};
\addplot[name path global=thirdline,very thick, domain=0:17]{16-2*x};
\addplot[name path global=fourthline,very thick, domain=0:17]{7};

% feasible area
\fill[name intersections={of=xaxis and yaxis,by=point1},
name intersections={of=secondline and yaxis,by=point2},
name intersections={of=firstline and secondline,by=point3},
name intersections={of=firstline and thirdline,by=point4},
name intersections={of=thirdline and xaxis,by=point5},
][very thick,draw=orange,pattern=crosshatch dots,pattern color=green!60!white](point1)--(point2)--(point3)--(point4)--(point5)--(point1);

% intersection points name        
\node[] at (axis cs:0.5,0.7){\small{$A$}};
\node[] at (axis cs:0.5,3.9){\small{$B$}};
\node[] at (axis cs:3,5.2){\small{$C$}};
\node[] at (axis cs:6.8,3.9){\small{$D$}};
\node[] at (axis cs:8.2,0.7){\small{$E$}};

\end{axis}
\end{tikzpicture}

在此处输入图片描述

大部分代码取自此处的一个回应(cmhughes): 如何绘制不平等区域? 感谢您的任何见解。

编辑:根据 marmot 的建议,我在序言中添加了 \pgfplotsset{compat=1.15}(或任何支持的版本),从而解决了这个问题。

相关内容