3

I was trying to shade the intersection of the following 3 lines, but don't know how to do that. Apparently, the coordinates of the 3 points of the triangle area are identifiable. Could anyone please help me with that? Thank you in advance.enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x$,
    ylabel = {$f(x)$},
    ymin=0, ymax=5
]
%Below the red parabola is defined
\addplot [name path=A][
    domain=0:4, 
    samples=10, 
    color=red,
]
{2-0.5*x};
\addlegendentry{A: 2-0.5*x}
%Here the blue parabloa is defined
\addplot [name path=B][
    domain=0:4, 
    samples=10, 
    color=blue,
    ]
    {(15-5*x)/3};
\addlegendentry{B: (15-5*x)/3}

\addplot [name path=C][
    domain=0:4, 
    samples=10, 
    color=yellow,
    ]
    {5*x-15};
\addlegendentry{C: 5*x-15}

\def\firstellipse{(-4,0) ellipse [x radius=8, y radius=3, rotate=45]}


\end{axis}
\end{tikzpicture}
\end{document}
Bobyandbob
  • 4,899
  • 1
    Please do not post fragments of code only. Your example misses the preamble with the needed packages to be loaded –  Oct 29 '17 at 16:25
  • 2
    Off-topic: Please, go to your previous question and accept the given answer – koleygr Oct 29 '17 at 17:03

2 Answers2

8

Here is a way:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x$,
    ylabel = {$f(x)$},
    ymin=0, ymax=5
]
%Below the red parabola is defined
\addplot [name path=A][
    domain=0:4, 
    samples=10, 
    color=red,
]
{2-0.5*x};
\addlegendentry{A: 2-0.5*x}
%Here the blue parabloa is defined
\addplot [name path=B][
    domain=0:4, 
    samples=10, 
    color=blue,
    ]
    {(15-5*x)/3};
\addlegendentry{B: (15-5*x)/3}

\addplot [name path=C][
    domain=0:4, 
    samples=10, 
    color=yellow,
    ]
    {5*x-15};
\addplot[blue!60] fill between[of=A and B,soft clip={domain=2.6:3}];
\addplot[blue!60,draw] fill between[of=C and A,soft clip={domain=3:3.1}];
\addlegendentry{C: 5*x-15}

\def\firstellipse{(-4,0) ellipse [x radius=8, y radius=3, rotate=45]}


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

I didn't solved the equations to find real points, and I suggest you to do it.

Output:

enter image description here

PS: If the above answers to your question, your question is duplicate of : pgfplots fillbetween with multiple curves

koleygr
  • 20,105
5

With \usetikzlibrary{intersections} you could (1.) compute the intersection points a,b and c and then (2.) fill the area between them.

intersection points:

\node[coordinate, name intersections = {of = A and B}] (a) at  (intersection-1) {};
\node[coordinate, name intersections = {of = A and C}] (b) at  (intersection-1) {};
\node[coordinate, name intersections = {of = B and C}] (c) at  (intersection-1) {};

Fill area

\fill[red](a) -- (b) -- (c);

enter image description here

MWE:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x$,
    ylabel = {$f(x)$},
    ymin=0, ymax=5
]
%Below the red parabola is defined
\addplot [name path=A][
    domain=0:4, 
    samples=10, 
    color=red,
]
{2-0.5*x};
\addlegendentry{A: 2-0.5*x}
%Here the blue parabloa is defined
\addplot [name path=B][
    domain=0:4, 
    samples=10, 
    color=blue,
    ]
    {(15-5*x)/3};
\addlegendentry{B: (15-5*x)/3}

\addplot [name path=C][
    domain=0:4, 
    samples=10, 
    color=yellow,
    ]
    {5*x-15};
\addlegendentry{C: 5*x-15}

\def\firstellipse{(-4,0) ellipse [x radius=8, y radius=3, rotate=45]}
%% NEW
\node[coordinate, name intersections = {of = A and B}] (a) at  (intersection-1) {};
\node[coordinate, name intersections = {of = A and C}] (b) at  (intersection-1) {};
\node[coordinate, name intersections = {of = B and C}] (c) at  (intersection-1) {};
\fill[red](a) -- (b) -- (c);
%%
\end{axis}
\end{tikzpicture}
\end{document}
Bobyandbob
  • 4,899