1

Hello I am creating a plot using tikz I have created 3 functions on a plot and took the area above each of them using drawge. My question is how I can fill the area above intersection of two functions ? or maybe I should create a polygon with the point of the intersection as one of the points of the polygon? The second part of question is why on x axis the part of the axis is transparent not black? Thank You in advance for answers

the plot looks now like this :

enter image description here

the picture below shows how I want it to look like enter image description here

here is my current code:

\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}
\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{
  only axis on top/.style={
axis on top=false,
after end axis/.code={
  \pgfplotsset{
    axis line style=opaque,
    ticklabel style=opaque,
    tick style=opaque,
    grid=none,
  }%
  %\pgfplotsdrawaxis
    },
  },
    }
\newcommand{\drawge}{%
  -- (rel axis cs:1,0) -- (rel axis cs:1,1) -- (rel axis cs:0,1) \closedcycle
}
\newcommand{\drawle}{%
  -- (rel axis cs:1,1) -- (rel axis cs:1,0) -- (rel axis cs:0,0) \closedcycle
}



 \begin{tikzpicture} [scale=1.5]
 \begin{axis}[only axis on top,
axis line style=very thick, 
axis x line=bottom, 
axis y line=left, 
ymin=0,
ymax=20,
xmin=0,
xmax=20, 
xlabel=$y_1$,
ylabel=$y_2$,
grid=major, 
ytick={-5,0, 5,10,15,20},
xtick={-5,0,5,10,15,20},
  ]   
       \addplot[
  very thick,
  fill=red!10,
  domain=-5:20,
] {(29-6*x)} \drawge;  

  \addplot[
  very thick,
  fill=red!10,
  domain=-5:20,
] {(29-6*x)} \drawge;  


\addplot[
  very thick,
  fill=yellow!10,
  domain=-5:20,
    ] {(16-4*x)/2} \drawge; 

\addplot[very thick, domain=-5:20] {(24-x)/5};
  \addplot[very thick, domain=-5:20] {(29-6*x)};
\addplot[very thick, domain=-5:20] {(16-4*x)/2};
  \end{axis}
 \end{tikzpicture}
1

You don't need those extra plots, which by the way are creating weird artifacts. Therefore, I have some suggestions:

  • Prefer to define a particular version for your pgfplots. If you set it to newest and then there is an update, it might break your document. Choose it manually like compat=1.13.
  • Use the fillbetween pgfplots library to fill between plots and paths.
  • axis line style={shorten >=-10pt}, makes the axis lines slightly longer so that you have the arrow coming out of the grid.
  • x axis line style={name path=xaxis} sets the path name for the x axis.
  • You can do the same with any path by writing name path=<name> in the plot/path options.
  • If you want the grid to be visible, you have two ways: (1) you use regular colors but with opacity or (2) you place the grid on top (in the axis options).

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\makeatletter
\newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis}
\makeatother
\pgfplotsset{
  only axis on top/.style={
axis on top=false,
after end axis/.code={
  \pgfplotsset{
    axis line style=opaque,
    ticklabel style=opaque,
    tick style=opaque,
    grid=none,
  }%
  %\pgfplotsdrawaxis
    },
  },
    }

\begin{document}
\begin{tikzpicture}%[scale=1.5]
\begin{axis}[only axis on top,
axis line style=very thick, 
axis x line=bottom, 
axis y line=left, 
ymin=0,
ymax=20,
xmin=0,
xmax=20, 
xlabel=$y_1$,
ylabel=$y_2$,
axis line style={shorten >=-10pt},
grid=major, 
ytick={-5,0, 5,10,15,20},
xtick={-5,0,5,10,15,20},
x axis line style={name path=xaxis}
  ]    

\path[name path=top] (0,20) -- (20,20); % defines an invisible path at the top of the grid, could be improved.

    \addplot[very thick, name path=a, domain=0:20] {(24-x)/5};
    \addplot[very thick, name path=b, domain=0:20] {(29-6*x)};
    \addplot[very thick, name path=c, domain=0:20] {(16-4*x)/2};

\addplot[fill=red!10] fill between[
    of=b and top,
    split,
    every even segment/.style={
        fill=none
    }
];

\addplot[fill=none] fill between[
        of=a and c,
        split,
        every segment no 1/.style={
            fill=yellow!10
        }
    ];

\addplot[fill=yellow!10] fill between[
        of=b and c,
    ];

\end{axis}
\end{tikzpicture}
\end{document}
1

Metapost is often more flexible for this sort of semi-numerical diagram. Here I've shown it wrapped up in luamplib, which requires lualatex to process.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
% first define the 20x20 box
path box;
box = unitsquare scaled 5cm;

% now define the three lines
path m[];
m1 = point -8/20 of box -- point   4/20 of box;
m2 = point -5/20 of box -- point  21/20 of box;
m3 = point  5/20 of box -- point -21/20 of box;

% now define the two regions to be filled
path r[];
r1 = point 2 of box -- point 3 of box -- m1 -- point 1 of box -- cycle;
r2 = point 2 of box -- point 1 of m3 -- m2 intersectionpoint m3 -- point 1 of m2 -- cycle;

% fill the regions
fill r1 withcolor 0.8[red+green,white];
fill r2 withcolor 0.8[blue, white];

% draw the lines
draw m1; draw m2; draw m3;

% mark the axes
drawarrow point 0 of box -- point 1 of box scaled 1.08;
drawarrow point 0 of box -- point 3 of box scaled 1.08;

label.llft("$0$", origin);
for i = 5 step 5 until 20: 
   draw (down--up)    shifted point  i/20 of box;
   draw (left--right) shifted point -i/20 of box;
   label.bot("$" & decimal i & "$", point  i/20 of box);
   label.lft("$" & decimal i & "$", point -i/20 of box);
endfor

label("$y_1$", point 0.5 of box shifted 20 down);
label(textext("$y_2$") rotated 90, point 3.5 of box shifted 24 left);

endfig;
\end{mplibcode}
\end{document}

I note one slightly irritating wrinkle that despite having set \mplibtextextlabel you still need to explicitly add the textext() macro call if you want to apply a transformation to the resulting picture.

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.