1

Hope you're safe!

I'm new to making graphs in pgfplots, and I've made the following graph (code below):

enter image description here

What I'd like to do is to shade the area of the graph corresponding to all the points which lie above the black, red, and green lines, but within the co-ordinates' domain and range. Does anyone know how to do this? Thanks!

\documentclass[12pt]{article}

\usepackage{tikz-cd} %diagrams
\usepackage{pgfplots} %graphs

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
scaled ticks=false,
width=14.9cm,
xmin=0,xmax=0.1,
ymin=0,ymax=0.1,
axis x line=middle,
axis y line=middle,
axis line style= ->,
xlabel={$\delta_{I}$},
ylabel={$\delta_{D}$},
y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    /tikz/.cd
},
x tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    /tikz/.cd
},
x label style={at={(axis description cs:1,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,1)},anchor=south},
]
\addplot expression[black,mark=none,domain=0:.1,very thick]{x}
node[pos=0.95,anchor= south east]{$\delta_{D}=\delta_{I}$};
\addplot expression[red,mark=none,domain=0:.1,very thick]{9/13*x+4/195}
node[pos=0.75,anchor= north west]{$\delta_{D}=\frac{9}{13}\delta_{I} + \frac{4}{195}$};
\addplot expression[blue,mark=none,domain=0:.1,very thick]{-1/2*x+1/30}
node[pos=0.5,anchor= south west]{$\delta_{D}=-\frac{1}{2}\delta_{I}$ + $\frac{1}{30}$};
\addplot expression[green,mark=none,domain=0:.1,very thick]{-2*x+1/15}
node[pos=0.05,anchor= south west]{$\delta_{D}=-2\delta_{I} + \frac{1}{15}$};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

1 Answer 1

1

Just compute the intersections and fill a polygon.

\documentclass[12pt]{article}

\usepackage{pgfplots} %graphs
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[set layers,
scaled ticks=false,
width=14.9cm,
xmin=0,xmax=0.1,
ymin=0,ymax=0.1,
axis x line=middle,
axis y line=middle,
axis line style= ->,
xlabel={$\delta_{I}$},
ylabel={$\delta_{D}$},
y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    /tikz/.cd
},
x tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    /tikz/.cd
},
x label style={at={(axis description cs:1,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,1)},anchor=south},
]
\addplot[name path=black] expression[black,mark=none,domain=0:.1,very thick]{x}
node[pos=0.95,anchor= south east]{$\delta_{D}=\delta_{I}$}
coordinate[pos=1] (tr);
\addplot[name path=red] expression[red,mark=none,domain=0:.1,very thick]{9/13*x+4/195}
node[pos=0.75,anchor= north west]{$\delta_{D}=\frac{9}{13}\delta_{I} + \frac{4}{195}$};
\addplot expression[blue,mark=none,domain=0:.1,very thick]{-1/2*x+1/30}
node[pos=0.5,anchor= south west]{$\delta_{D}=-\frac{1}{2}\delta_{I}$ + $\frac{1}{30}$};
\addplot[name path=green] expression[green,mark=none,domain=0:.1,very thick]{-2*x+1/15}
coordinate[pos=0] (tl)
node[pos=0.05,anchor= south west]{$\delta_{D}=-2\delta_{I} + \frac{1}{15}$};
\pgfonlayer{pre main}
\path[fill=blue!20,name intersections={of=green and red,by=i1},
name intersections={of=red and black,by=i2}]
(tl) -- (i1) -- (i2) -- (tr) -| cycle;
\endpgfonlayer
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

2
  • Is there any way to adjust the shading such that it doesn't block out the vertical axis? Thanks! Apr 7, 2020 at 15:17
  • 1
    @JamesSkinner Sure. Replace \pgfonlayer{pre main} by \pgfonlayer{axis background}.
    – user194703
    Apr 7, 2020 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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