0

Using pgfplots I have a region enclosed within three lines and the axes. I used the intersections library as indicated by this solution. The problem is that the intersections do not all seem to be correct. Since I am intersecting just lines with lines, there should be one intersection per pair of lines. Below is an MWE that produces the figure shown. I want the blue region to be the darkest red region that can be seen just peeping out from behind.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture} 
    \begin{axis}[
        axis lines=left,
        every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
        every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
        enlargelimits={value=0.2,upper},
        scaled ticks=false, 
        minor x tick num=1,
        domain=0:8,
        ]
\addplot[name path global=line1, black, fill=red, fill opacity=0.2]
{6}node[black,opacity=1,above]{line1}\closedcycle;

\draw[name path global=line2] ({axis cs:4,0}|-{rel axis cs:0,0}) -- ({axis cs:4,0}|-{rel axis cs:0,1})node[pos=0.8,black,opacity=1,right]{line2};
\addplot[fill=red, fill opacity=0.2,domain=0:4] {12}\closedcycle;

\addplot[name path global=line3, black, fill=red, fill opacity=0.2]
{(18-3*x)/2}node[sloped,pos=0.63,opacity=1,above]{line3}\closedcycle;


\fill[name intersections={of=line1 and line2,by=point12},
      name intersections={of=line1 and line3,by=point13},
      name intersections={of=line2 and line3,by=point23},
      ][fill=blue](0,0)--(0,6)--(point13)node{13}--(point23)node{23}--(4,0)--(0,0);

\end{axis} 
\end{tikzpicture}%  

\end{document}

enter image description here

3

You have 2 options in this case to fix the part that goes below 0:

  1. Add axis cs: to the points in the fill command
  2. The most recent version of pgfplots is 1.11. By changing the version, I am able to get the result you want with no other changes. This is probably because in 1.11, the default coordinate system is axis cs.

I'll show #1 here, since that's marginally more complicated, and uses the same version you are using. Note that combining #1 and #2 causes no problems, and it should probably be preferred to use the most recent version in general.

In addition, the node 13 is misplaced because of the \closedcycle macro, there are multiple intersection points. In this case, you need the second intersection of line1 and line3, which we can get by naming the first intersection point something else, and naming the second one by point13.

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        axis lines=left,
        every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
        every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
        enlargelimits={value=0.2,upper},
        scaled ticks=false,
        minor x tick num=1,
        domain=0:8,
        ]
\addplot[name path global=line1, black, fill=red, fill opacity=0.2]
{6}node[black,opacity=1,above]{line1}\closedcycle;

\draw[name path global=line2] ({axis cs:4,0}|-{rel axis cs:0,0}) -- ({axis cs:4,0}|-{rel axis cs:0,1})node[pos=0.8,black,opacity=1,right]{line2};
\addplot[fill=red, fill opacity=0.2,domain=0:4] {12}\closedcycle;

\addplot[name path global=line3, black, fill=red, fill opacity=0.2]
{(18-3*x)/2}node[sloped,pos=0.63,opacity=1,above]{line3}\closedcycle;


\fill[name intersections={of=line1 and line2,by=point12},
      name intersections={of=line1 and line3,by={a,point13}},
      name intersections={of=line2 and line3,by=point23},
      ][fill=blue](axis cs:0,0)--(axis cs:0,6)--(point13)node{13}--(point23)node{23}--(axis cs:4,0)--(axis cs:0,0);

\end{axis}
\end{tikzpicture}%

\end{document}

enter image description here

  • Your code produces the picture above, right? If so, then point13 is, like my code, not the intersection of the line1 and line3. – Geoff Feb 2 '15 at 14:39
  • @Geoff Oh I didn't even notice that! The problem is due to the \closedcycle part, if you remove both of those the problem is solved but the fill goes away. Let me work a bit to fix it... – darthbith Feb 2 '15 at 15:58
  • @Geoff Should be fixed now! – darthbith Feb 2 '15 at 16:05
  • I get it now, the \closedcycle is obviously part of the path, hence "lines" have more than one intersection point! – Geoff Feb 2 '15 at 16:11
  • Right. You can read more about this in section 13.3.2 of the PGF 3.0.0 manual :-) – darthbith Feb 2 '15 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.