2

I'm trying to shade the feasible region ABCDE but for some reason, only get a weird line. This is the code:

\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}

enter image description here

Most of the code is taken from one of the responses (cmhughes) here: How to draw the region of inequality? Thanks for any insights.

EDIT: Following marmot suggestion, I've added \pgfplotsset{compat=1.15} (or whatever version supported) to the preamble and that solved the problem.

Stefan Pinnow
  • 29,535
  • 5
    Welcome to TeX.SE! I get an area if I compile with the preamble \documentclass[tikz,border=3.14mm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.16} \usetikzlibrary{intersections,patterns} \begin{document} and of course put \end{document} at the end. To understand where your problem comes from, you need to provide the complete preamble, i.e. an MWE. –  Aug 24 '18 at 15:57
  • 1
    If you want to connect A-B-C-D-E, you need to add name intersections={of=firstline and secondline,by=point2a} (which represents C) and add this node to your drawing. Adding --cycle may also help. However, I also cannot reproduce your problem and the area is drawn nicely with marmot’s minimal wrapper. – Jasper Habicht Aug 24 '18 at 16:34
  • I just rejected an edit by a user with the same name, but different profile picture. May it be that you have two accounts? I've heard that you could merge them. (And if that's really you, I don't understand the comment on version 1.15 vs. 1.16, I get areas in both versions. Or do you just mean that you do not yet have version 1.16?) BTW, I'd be really curious to see the preamble that caused that strange result... –  Aug 24 '18 at 18:57
  • Thanks, just merge the accounts. I am using the exact preamble you suggested. Again, only 1.15 gives me the correct area. I'm using Overleaf v2, perhaps it only uses 1.15? When I go to Overleaf v1 I get an error that I can use maximum 1.14 (which gives me the right area). The main thing is that adding compat command solves the problem. Thanks again. – user169054 Aug 25 '18 at 07:11
  • Jasper Habicht, Indeed I forgot point C. Thanks for that. – user169054 Aug 25 '18 at 07:19
  • 4
    I'm voting to close this question as off-topic because solved in comment adding \pgfplotsset{compat=1.15}. – CarLaTeX Feb 13 '19 at 19:57

0 Answers0