0

I need to colour the area between the first and the second line of the graph in green and the area between the second and the third line of the graph in red: I am trying to do this with fillbetween, as I later I have to this with a curve. I had a look at this question, but I couldn't understand at all what soft clip does, and, quite honestly, I am a bit lost... I provide an example of what I'd want, which is a plot I coded uniquely with tikz. The second picture is where I am stuck using pgfplot.

EDIT: I kinda understood, but I still have the problem that axis are also coloured, and here I can't fill the path before drawing the functions...

Example of the output I'm looking for. enter image description here

enter image description here

\documentclass[12pt]{article}
    \usepackage[utf8]{inputenc}
    \usepackage{xcolor}
    \usepackage{tikz}
    \usepackage{pgfplots}
    \pgfplotsset{width=9cm,compat=1.10}
    \usepgfplotslibrary{fillbetween}
    \begin{document}
        \begin{figure}[t]
\centering
\begin{tikzpicture}
\begin{axis}[
title = {$\delta = 0.9$},
xlabel = {$\xi$},
ylabel = {$c(\xi)$},
xmin = 0,
xmax = 1,
ymin = 0.17,
ymax=  1,
]
\addplot[
name path = A,
domain= 0:1,
samples= 100,
line width = 1.2 pt, 
]
{(1+x)/2};
\node at (axis cs:  .8,  .84){\footnotesize$C(\xi)$};
\addplot[
name path  = B,
domain= 0:1,
samples= 100,
style = dashed,
]
{(1/4 + (0.9 +x)/2};
\node at (axis cs: 0.2,0.87){\footnotesize$\bar{c}(\xi;\delta)$};
\addplot[
name path = C,
domain= 0:1,
samples= 100,
style = dashed,]
{(3/4 + (-0.9 +x)/2};
\node at (axis cs:  .8,  .63){\footnotesize$\underline{c}(\xi;\delta)$};
\addplot[green!20] fill between [of = A and C, soft clip={domain=0:1}];
\addplot[red!20] fill between [of = B and A, soft clip={domain=0:1}];
\end{axis}
\end{tikzpicture}
\label{fig:SustProf}
\caption{Sustainability of the ABS with a competitive fringe of retailers for fixed b.}
\end{figure}
    \end{document}
  • Sorry, I am usure what you would like the output to be. Should it the colored areas be clipped at a specific x-value? Then you should maybe try soft clip={domain=0:0.6}. The value for the key domain takes two numbers (separated by a colon) which denote the x-value where the fill between area should start and where it should end. – Jasper Habicht Nov 04 '22 at 17:21

1 Answers1

1

I am not sure whether his is what you try to achieve, but if you want the colored area created by fill between to be clipped at specific x-values, then you should put these x-values as values of the domain key of the soft clip option, such as soft clip = {domain = 0:0.6} (denoting that the area starts at x = 0 and ends at x = 0.6). The value for the key domain takes two numbers (separated by a colon :) which denote the x-values where the area created by fill between should start and where it should end.

I removed samples = 100, since you don't need that much samples for just a straight line. It will only increase compilation time. Also, if you load pgfplots, there is no need to additionally load tikz or the xcolor package (the latter will already be loaded by both the tikz as well as the pgfplots package).

To place tick marks above the colored area, use the option axis on top on the axis:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{width=9cm, compat=1.10}
\usepgfplotslibrary{fillbetween}

\begin{document} \begin{figure}[t] \centering \begin{tikzpicture} \begin{axis}[ title = {$\delta = 0.9$}, xlabel = {$\xi$}, ylabel = {$c(\xi)$}, xmin = 0, xmax = 1, ymin = 0.17, ymax = 1, axis on top ] \addplot[ name path = A, domain = 0:1, line width = 1.2 pt, ] {(1+x)/2}; \node at (axis cs: .8, .84){\footnotesize$C(\xi)$}; \addplot[ name path = B, domain = 0:1, style = dashed, ] {(1/4 + (0.9 +x)/2}; \node at (axis cs: .2, .87){\footnotesize$\bar{c}(\xi;\delta)$}; \addplot[ name path = C, domain = 0:1, style = dashed,] {(3/4 + (-0.9 +x)/2}; \node at (axis cs: .8, .63){\footnotesize$\underline{c}(\xi;\delta)$};

\addplot[green!20] fill between [of = A and C, soft clip = {domain = 0:0.6}]; \addplot[red!20] fill between [of = B and A, soft clip = {domain = 0:0.6}];

\end{axis} \end{tikzpicture} \label{fig:SustProf} \caption{Sustainability of the ABS with a competitive fringe of retailers for fixed b.} \end{figure}

\end{document}

enter image description here

  • 1
    Hi Jasper, I basically answered myself: the last figure was the output I wanted, and thanks for the help by the way!

    I still can' figure out how to add an area legend in this case

    – Matteo Bulgarelli Nov 04 '22 at 17:43