1

Please consider this MWE:

\documentclass{article}

\usepackage[english]{babel} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc}

\usepackage{pgfplots} \usetikzlibrary{arrows,overlay-beamer-styles,babel,arrows.meta, patterns} \pgfplotsset{compat=1.8}

% ======================== % From https://tex.stackexchange.com/a/76014/152550 \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{document}

\begin{tikzpicture} \begin{axis}[ legend pos=outer north east, axis lines = center, xticklabel style = {font=\tiny}, yticklabel style = {font=\tiny}, xlabel = $x$, ylabel = $y$, legend style={cells={align=left}}, legend cell align={left}, xticklabel=\empty, yticklabel=\empty, ] \addplot[very thick,red] {0.5x+1}; \addplot[very thick,red] {-2x+3}; %\addplot[draw=none,pattern=vertical lines,green,opacity=0.3] {0.5x+1}\drawge; %\addplot[draw=none,pattern=vertical lines,blue,opacity=0.3] {0.5x+1}\drawle; %\addplot[draw=none,pattern=vertical lines,orange,opacity=0.3] {-2x+3}\drawge; %\addplot[draw=none,pattern=vertical lines,violet,opacity=0.3] {-2x+3}\drawle; \end{axis} \end{tikzpicture}

\end{document}

MWE output

I would like to fill the 4 regions that are divided into the plane, with no-overlapping of solid colors (including the red lines). If we uncomment the last four \addplot we get:

What I have done

As you can see:

  • The four \addplot colors are overlapping, and
  • The red lines overlap with the 4 colors.

How can we prevent the overlapping between area colors and line colors?

P.S. I should be able to add a 3rd red line to the above, and paint the regions with 7 different colors in total.

manooooh
  • 3,203
  • 2
  • 21
  • 46
  • https://tex.stackexchange.com/questions/75933/how-to-draw-the-region-of-inequality?noredirect=1&lq=1 – js bibra Jul 08 '20 at 01:36
  • @jsbibra hello and thanks for the comment!! That's the link I put on the code. However, in the accepted answer they treat overlapping with patterns, not with solid colors. – manooooh Jul 08 '20 at 01:37
  • please see if the answer meets your requirement – js bibra Jul 08 '20 at 05:18
  • can you elaborate what is wrong with the answer given by @ZhiyuanLck--and a handrawn sketch of the desired output--probably the question has not been understood correctly – js bibra Jul 13 '20 at 13:20

2 Answers2

3

UPDATE

I find a better way to color regions: find vertices of region and fill the path on axis background layer with the specification set layers=standard. enter image description here

\documentclass{article}

\usepackage{pgfplots} \usetikzlibrary{intersections} \usepgfplotslibrary{fillbetween} \pgfplotsset{compat=1.17}

\begin{document} \begin{tikzpicture} [ pin distance=2mm, every pin edge/.style={thick,-}, ] \begin{axis}[ set layers=standard, legend pos=outer north east, axis lines = center, xticklabel style = {font=\tiny}, yticklabel style = {font=\tiny}, xlabel = $x$, ylabel = $y$, legend style={cells={align=left}}, legend cell align={left}, xticklabel=\empty, yticklabel=\empty, ] \addplot[name path=l1, very thick,red] {0.5x+1} coordinate [pos=0] (l1-0) coordinate [pos=1] (l1-1) node [pos=0.1, pin=below:{$l_1$}] {}; \addplot[name path=l2, very thick,teal] {-2x+3} coordinate [pos=0] (l2-0) coordinate [pos=1] (l2-1) node [pos=0.1, pin=below:{$l_2$}] {}; \addplot[name path=l3, very thick,brown] {3*x-4.3} coordinate [pos=0] (l3-0) coordinate [pos=1] (l3-1) node [pos=0.2, pin=right:{$l_3$}] {};

% intersection point \path[name intersections={of=l1 and l2, name=i}]; \path[name intersections={of=l1 and l3, name=j}]; \path[name intersections={of=l2 and l3, name=k}]; \node[above] at (i-1) {$i_1$}; \node[above] at (j-1) {$j_1$}; \node[below] at (k-1) {$k_1$};

\tikzaliascoordinatesystem{r}{rel axis} \pgfonlayer{axis background} \fill[red!10] (l2-0) -- (i-1) -- (l1-0); \fill[green!10] (l2-0) -- (i-1) -- (j-1) -- (l3-1) -- (r cs:1, 1); \fill[blue!10] (j-1) -- (l1-1) -- (l3-1); \fill[yellow!10] (l3-0) -- (k-1) -- (i-1) -- (l1-0); \fill[cyan!20] (l3-0) -- (k-1) -- (l2-1) -- (r cs:1, 0); \fill[teal!20] (l2-1) -- (k-1) -- (j-1) -- (l1-1); \fill[violet!50] (i-1) -- (j-1) -- (k-1); \endpgfonlayer

\end{axis} \end{tikzpicture}

\end{document}

Old answer

Use pgfplotslibrary fillbetween. Divide the region to 6 subregions to fill. enter image description here

\documentclass{article}

\usepackage{pgfplots} \usetikzlibrary{intersections} \usepgfplotslibrary{fillbetween} \pgfplotsset{compat=1.17}

\begin{document} \begin{tikzpicture} \begin{axis}[ legend pos=outer north east, axis lines = center, xticklabel style = {font=\tiny}, yticklabel style = {font=\tiny}, xlabel = $x$, ylabel = $y$, legend style={cells={align=left}}, legend cell align={left}, xticklabel=\empty, yticklabel=\empty, ] \addplot[name path=l1, very thick,red] {0.5x+1}; \addplot[name path=l2, very thick,red] {-2x+3};

% fill left and right \addplot[opacity=.3] fill between [ of=l1 and l2, split, every segment no 0/.style=blue, every segment no 1/.style=orange, ];

% intersection point \path[name intersections={of=l1 and l2, name=i}];

% upper and lower bound lines \path[name path=upper] (rel axis cs:0, 1) -- (rel axis cs:1, 1); \path[name path=lower] (rel axis cs:0, 0) -- (rel axis cs:1, 0);

% fill upper left \addplot [green, opacity=.3] fill between [ of=l2 and upper, % rectangle must be constructed from left to right soft clip={(rel axis cs:0, 1) rectangle (i-1)}, ];

% fill upper right \addplot [green, opacity=.3] fill between [ of=l1 and upper, soft clip={(i-1) rectangle (rel axis cs:1, 1)}, ];

% fill lower left \addplot [violet, opacity=.3] fill between [ of=l1 and lower, soft clip={(rel axis cs:0, 0) rectangle (i-1)}, ];

% fill lower right \addplot [violet, opacity=.3] fill between [ of=l2 and lower, soft clip={(i-1) rectangle (rel axis cs:1, 0)}, ]; \end{axis} \end{tikzpicture}

\end{document}

ZhiyuanLck
  • 4,516
  • 1
    Thanks for the code!! It helps me. However I am unable to do it for additional line, like \addplot[name path=l3, very thick,red] {3*x-4.3};. I calculated 2 new intersection points, namely \path[name intersections={of=l1 and l3, name=j}]; and \path[name intersections={of=l2 and l3, name=k}]; but I can't draw it correctly. Is the same idea or there are new changes? – manooooh Jul 08 '20 at 21:38
1

Does this meet your rquirement--

enter image description here

\documentclass{article}

\usepackage{pgfplots} \usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis lines = center,
xticklabel style = {font=\tiny},
yticklabel style = {font=\tiny},
xlabel = $x$,
ylabel = $y$,
legend style={cells={align=left}},
legend cell align={left},
xticklabel=\empty,
yticklabel=\empty,
]
\addplot[very thick,name path=a,draw=none] {0.5*x+1};
\addplot[very thick,draw=none,name path=b] {-2*x+3};
\path [name intersections={of=a and b,by=c}];
\node [fill=blue,inner sep=2pt,label=-90:$C$] at (c) {};

\path[name path=lower,
%thick,draw=red,
intersection segments={
    of=a and b,
    sequence=B0 -- A1,
}
]; 
\path[name path=upper,
%thick,draw=red,
intersection segments={
    of=a and b,
    sequence=B1 -- A0,
}
]; 

\addplot[red!70, opacity=.5]   fill between[of=lower and a];
    \addplot[blue!70, opacity=.5]   fill between[of=lower and b];   
            \addplot[green!70, opacity=.5]   fill between[reverse=false,of=lower 
             and a];
                    \addplot[green!70!yellow!30, opacity=.5]   fill 
                     between[of=upper and b];
\end{axis}

\end{tikzpicture}

\end{document}

js bibra
  • 21,280
  • Thanks for the answer!! There is nothing wrong about your answer. I selected the other because it seems more easy to implement for me, but the same output can be achieved with yours! (That's why I upvoted your question.) – manooooh Jul 13 '20 at 17:38
  • 1
    i asked becos you have placed a bounty --is there some other requirement – js bibra Jul 14 '20 at 00:32
  • I need the same but with 3 lines instead of 2. That's the other requirement if you want to do it. Thanks!! – manooooh Jul 14 '20 at 01:06