4

This question already has an answer here:

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}

marked as duplicate by koleygr, Mensch, CarLaTeX, TeXnician, Stefan Pinnow Oct 29 '17 at 18:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Please do not post fragments of code only. Your example misses the preamble with the needed packages to be loaded – user31729 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
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

  • The real domain of second fill is 3:17/5.5 – koleygr Oct 29 '17 at 17:00
  • Thank you! You've answered my question and this is a duplicate of that link. I didn't understand that before, sorry~ – Darcy Pu Oct 29 '17 at 17:31
  • Welcome... Happy TeXing... – koleygr Oct 29 '17 at 17:32
4

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}

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