2

I have a problem with coloring a area with three restrictions. The area I want to color is between the points: (0,0), (0,1.5), (2,1.5), (2.5,1.25), (2.5,0), (0,0). As you can see I've managed to do something but no matter what I try either the big triangle or the little one will be colored. I hope you can help. enter image description here

\begin{tikzpicture}
 \begin{axis}[
    axis lines = left,
    width=10cm, height=8cm,
    ymin=0,
    xmin=0,
    ymax=3,
    xmax=6,
    xticklabels={,,},
    yticklabels={,,},
    clip=false
]   
\addplot[smooth][domain=0:5, color=black,line width=0.4mm]{1.5}; 
\addplot[smooth][name path=f,domain=0:5, color=black, line width=0.4mm]{(5-x)/2};    

\addplot[smooth] coordinates {(2.5,-0.1) (2.5,3)} [color=black, line width=0.4 mm];
\path[name path=axis](axis cs:0,0) -- (axis cs:2.5,0);
\addplot [
    thick,
    color=brown,
    fill=blue, 
    fill opacity=0.5
]
fill between[
    of=axis and f,
    soft clip={domain=0:2.5},
];

  \node at (axis cs:-0.3,-0.3){$(0,0)$};
  \node at (axis cs:2.5,-0.3){$(2.5,0)$};
  \node at (axis cs:5,-0.3){$(5,0)$};
  \node at (axis cs:-0.6,2.5){$(0,2.5)$};
  \node at (axis cs:1.7,1.3){$(2,1.5)$};
  \node at (axis cs:3.2,1.7){$(2.5,1.5)$};
  \node at (axis cs:3.3,1.25){$(2.5,1.25)$};
  \node at (axis cs:-0.6,1.5){$(0,1.5)$};

\fill (0,0) circle[radius=2pt];
\fill (2.5,0) circle[radius=2pt];
\fill (5,0) circle[radius=2pt];
\fill (0,2.5) circle[radius=2pt];
\fill (2,1.5) circle[radius=2pt];
\fill (2.5,1.5) circle[radius=2pt];
\fill (2.5,1.25) circle[radius=2pt];
\fill (0,1.5) circle[radius=2pt];

\addplot[dashed][domain=-1/2:1/2, color=black, line width=0.4mm]{-x/3};
\end{axis}
\end{tikzpicture}
Bobyandbob
  • 4,899
  • Welcome to TeX.SX! Please provide a compilable document, not just a fragment. - YOu could use \fill[red,draw=red] plot coordinates { (0,0) (0,1.5) (2,1.5) (2.5,1.25) (2.5,0) } ; – Bobyandbob Feb 25 '18 at 14:09
  • @Bobyandbob with your edit to the title you are canceling my answer and making yours the only real and accepted answer to the question. I know that you didn't try to do that, but you did it my mistake. The points given in the question was to describe the problem (as far as I can understand) and not a real limitation ιfor the accepted answers. The way of the OP is closer to mine even if I suggest (from the first answer) your way too. (Please see here my opinion on editing the titles https://tex.meta.stackexchange.com/a/7412/120578) – koleygr Feb 25 '18 at 14:35
  • @koleygr Sorry. I only do not prefer the title with "Problem". I will make a rollback, your answer is fine. – Bobyandbob Feb 25 '18 at 14:40
  • @Bobyandbob, thanks... Yours is better in this particular case... Just want to mention the problem with editing the titles... My answer is just closer to the OPs initial approach (But a duplicate too of this question https://tex.stackexchange.com/q/344021/120578 if mine will be accepted or of another post (possibly) if yours will be accepted). (Didn't noticed the "Problem" there... We can just ask him to rephrase because the title is not good as you mentioned... We just can't) – koleygr Feb 25 '18 at 14:43
  • (We just can't decide without him what was his real request with this question) – koleygr Feb 25 '18 at 14:49
  • @koleygr i vote to close this question as duplicate, since there is no reaction by MadsMikkelsen. – Bobyandbob Aug 18 '18 at 19:26

2 Answers2

2

An option is to use the normal \fill comand with an list of coordinates of the boundary.

\fill[red,draw=blue] plot coordinates { (x1,y1)(x2,y2)(x3,y3)(x4,y4) ...)

Result:

enter image description here

MWE:

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[
    axis lines = left,
    width=10cm, height=8cm,
    ymin=0,
    xmin=0,
    ymax=3,
    xmax=6,
    xticklabels={,,},
    yticklabels={,,},
    clip=false
]   
\addplot[smooth][domain=0:5, color=black,line width=0.4mm]{1.5}; 
\addplot[smooth][name path=f,domain=0:5, color=black, line width=0.4mm]{(5-x)/2};    

\addplot[smooth] coordinates {(2.5,-0.1) (2.5,3)} [color=black, line width=0.4 mm];
\path[name path=axis](axis cs:0,0) -- (axis cs:2.5,0);

\fill[red,draw=blue] plot coordinates {
  (axis cs:0,0)
  (axis cs:0,1.5)
  (axis cs:2,1.5)
  (axis cs:2.5,1.25)
  (axis cs:2.5,0)
  (axis cs:0,0)
} ;
\node at (axis cs:-0.3,-0.3){$(0,0)$};
\node at (axis cs:2.5,-0.3){$(2.5,0)$};
\node at (axis cs:5,-0.3){$(5,0)$};
\node at (axis cs:-0.6,2.5){$(0,2.5)$};
\node at (axis cs:1.7,1.3){$(2,1.5)$};
\node at (axis cs:3.2,1.7){$(2.5,1.5)$};
\node at (axis cs:3.3,1.25){$(2.5,1.25)$};
\node at (axis cs:-0.6,1.5){$(0,1.5)$};

\fill (axis cs:0,0) circle[radius=2pt];
\fill (axis cs:2.5,0) circle[radius=2pt];
\fill (axis cs:5,0) circle[radius=2pt];
\fill (axis cs:0,2.5) circle[radius=2pt];
\fill (axis cs:2,1.5) circle[radius=2pt];
\fill (axis cs:2.5,1.5) circle[radius=2pt];
\fill (axis cs:2.5,1.25) circle[radius=2pt];
\fill (axis cs:0,1.5) circle[radius=2pt];

\addplot[dashed][domain=-1/2:1/2, color=black, line width=0.4mm]{-x/3};

\end{axis} \end{tikzpicture}
\end{document}
Bobyandbob
  • 4,899
1

According to my answer here: https://tex.stackexchange.com/a/398639/120578

You have to use separate commands for the first fill between and the second, because the first is the named A plot (Added name... See comment) until x=2 and then the f from x=2 until x=2.5.

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
 \begin{axis}[
    axis lines = left,
    width=10cm, height=8cm,
    ymin=0,
    xmin=0,
    ymax=3,
    xmax=6,
    xticklabels={,,},
    yticklabels={,,},
    clip=false
]   
\addplot[smooth][domain=0:5, color=black,line width=0.4mm,name path=A]{1.5};%Added name A 
\addplot[smooth][name path=f,domain=0:5, color=black, line width=0.4mm]{(5-x)/2};    

\addplot[smooth] coordinates {(2.5,-0.1) (2.5,3)} [color=black, line width=0.4 mm];
\path[name path=axis](axis cs:0,0) -- (axis cs:2.5,0);
%\addplot [% Replaced with below
%    thick,
%    color=brown,
%    fill=blue, 
%    fill opacity=0.5
%]
%fill between[
%    of=axis and f,
%    soft clip={domain=0:2.5},
%];
\addplot[thick,
  color=brown,
  fill=blue,
  fill opacity=0.5
] fill between[of=axis and A,soft clip={domain=0:2}];
\addplot[thick,
  color=brown,
  fill=blue,
  fill opacity=0.5
] fill between[of=axis and f,soft clip={domain=2:2.5}];
  \node at (axis cs:-0.3,-0.3){$(0,0)$};
  \node at (axis cs:2.5,-0.3){$(2.5,0)$};
  \node at (axis cs:5,-0.3){$(5,0)$};
  \node at (axis cs:-0.6,2.5){$(0,2.5)$};
  \node at (axis cs:1.7,1.3){$(2,1.5)$};
  \node at (axis cs:3.2,1.7){$(2.5,1.5)$};
  \node at (axis cs:3.3,1.25){$(2.5,1.25)$};
  \node at (axis cs:-0.6,1.5){$(0,1.5)$};

\fill (0,0) circle[radius=2pt];
\fill (2.5,0) circle[radius=2pt];
\fill (5,0) circle[radius=2pt];
\fill (0,2.5) circle[radius=2pt];
\fill (2,1.5) circle[radius=2pt];
\fill (2.5,1.5) circle[radius=2pt];
\fill (2.5,1.25) circle[radius=2pt];
\fill (0,1.5) circle[radius=2pt];

\addplot[dashed][domain=-1/2:1/2, color=black, line width=0.4mm]{-x/3};
\end{axis} \end{tikzpicture}
\end{document}

But in your case If you know the coordinates it is easier the way of @Bobyandbod's comment and answer:

Output:

enter image description here

koleygr
  • 20,105