# Pgfplots: how to fill bounded area under a curve using addplot and fill?

I am looking shade the bounded area under the graph of f(x)=x^2 from 0 to 1. For my other plots I was fine using the shading with coordinates because they were all polygons. I've come across a bunch of threads with similar situations but didn't see anything quite similar enough for me to copy and manipulate the code. I The image below is exactly how I want it with the clear exception of the shading outside the box. Thanks

\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}

\begin{figure}[!h]
\centering
\begin{tikzpicture}
\begin{axis}[enlargelimits=0.1]
fill opacity=0.05]coordinates {
(1, 1)
(1, 0)
(0, 0)  };
\node [rotate=48] at (axis cs:  .7,  .59) {$y=x^2$};
\node [rotate=90] at (axis cs:  1.05,  .25) {$x=1$};
\end{axis}
\end{tikzpicture}
\caption{$R=\left\{(x,y) \ | \ 0 \leq y \leq x^2, x\leq 1 \right\}$}
\end{figure} \\


Edit:

The first and second answers below are perfect. What if I wanted the shaded area under the curve to stop at a certain y-value, instead of extending all the way down to the x-axis? Say, it goes down to y=.2?

-
In my proposal, just change the clip command in the first scope environment to \clip (axis cs:0,0.2) -- (axis cs:1,0.2) -- (axis cs:1,1)-- cycle; – Jesse Mar 12 '14 at 13:08

This can be done by means of the fillbetween library which has been introduced in pgfplots 1.10 :

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.10}

\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
\begin{axis}[enlargelimits=0.1]

\path[name path=axis] (axis cs:0,0) -- (axis cs:1,0);

thick,
color=blue,
fill=blue,
fill opacity=0.05
]
fill between[
of=f and axis,
soft clip={domain=0:1},
];

\node [rotate=48] at (axis cs:  .7,  .59) {$y=x^2$};
\node [rotate=90] at (axis cs:  1.05,  .25) {$x=1$};
\end{axis}
\end{tikzpicture}
\end{document}


The basic idea is to have two labelled input paths, in our case the function as such and the path which resembles the other boundary (in our case the part of the axis from 0 to 1). Then, \addplot fill between can draw the area between these two input paths.

The fill between library can also draw intersection segments individually. This would allow you to fill only the area between y=0.2 and the function:

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.10}

\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}[enlargelimits=0.1]

\path[name path=axis] (axis cs:0,0.2) -- (axis cs:1,0.2);

thick,
color=blue,
fill=blue,
fill opacity=0.05
]
fill between[
of=f and axis,
split,
every segment no 0/.style={
%fill=none,
yellow,
},
];

\node [rotate=48] at (axis cs:  .7,  .59) {$y=x^2$};
\node [rotate=90] at (axis cs:  1.05,  .25) {$x=1$};
\end{axis}
\end{tikzpicture}
\end{document}


In this example, the second path (labelled axis) is at y=0.2 and we fill between f and axis. Clearly, this results in two segments. I told fillbetween to fill the first segment in yellow, but you can easily use fill=none to make it invisible.

In case you want to show the boundaries of the filled region, you can easily add draw to the option list of \addplot fill between.

-

Use \closedcycle for addplot:

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}

\begin{figure}[!h]
\centering
\begin{tikzpicture}

\begin{axis}[enlargelimits=0.1]
fill opacity=0.05]coordinates {
(1, 1)
(1, 0)
(0, 0)  };
\node [rotate=48] at (axis cs:  .7,  .59) {$y=x^2$};
\node [rotate=90] at (axis cs:  1.05,  .25) {$x=1$};
\end{axis}
\end{tikzpicture}
\caption{$R=\left\{(x,y) \ | \ 0 \leq y \leq x^2, x\leq 1 \right\}$}
\end{figure}
\end{document}


-
This is exactly what I asked for, thank you! What if I wanted the shaded area under the curve to stop at a certain y-value, instead of extending all the way down to the x-axis? Say, it goes down to y=.2? – Reck Mar 12 '14 at 5:01

Another approach is to clip and fill the plot twice. The first scope clips and fills (in blue) triangle containing the two curves, then the second scope clips and fills (in white) the area above x^2, yielding

Code

\documentclass{article}
\usepackage[papersize={12cm,10cm}]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\thispagestyle{empty}

\begin{document}

\begin{figure}[!hbtp]
\centering
\begin{tikzpicture}

\begin{axis}[enlargelimits=0.1]
\begin{scope}
\clip (axis cs:0,0) -- (axis cs:1,0) -- (axis cs:1,1)-- cycle;
\addplot [draw=none,fill=blue]coordinates { (1, 1)   (1, 0)   (0, 0)};
\end{scope}
\begin{scope}
\clip (axis cs:0,0) -- (axis cs:1,0) -- (axis cs:1,1.1)-- cycle;
\end{scope}
fill opacity=0.05]coordinates  {
(1, 1)
(1, 0)
(0, 0)  };
\node [rotate=48] at (axis cs:  .7,  .59) {$y=x^2$};
\node [rotate=90] at (axis cs:  1.05,  .25) {$x=1$};
\end{axis}
\end{tikzpicture}
\caption{$R=\left\{(x,y) \ | \ 0 \leq y \leq x^2, x\leq 1 \right\}$}
\end{figure}

\end{document}

-

Two more options:

1.- Divide your plot for x^2 in two plots, one without fill option and domain=-0.15:0 and a second one with fill option and domain=0:1.

\begin{tikzpicture}
\begin{axis}[enlargelimits=0.1]

fill opacity=0.05]coordinates {
(1, 1)
(1, 0)
(0, 0)  };
\node [rotate=48] at (axis cs:  .7,  .59) {$y=x^2$};
\node [rotate=90] at (axis cs:  1.05,  .25) {$x=1$};

\end{axis}
\end{tikzpicture}


2- Use new pgfplotslibrary (from recent pgfplots version 1.10) fillbetween to fill area between curves in desired domain. In this case you have to identify curves with a name path and after that plot (fill) area between them.

\documentclass{article}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{figure}[!h]
\centering
\begin{tikzpicture}
\begin{axis}[enlargelimits=0.1]

\path[name path=B]
(axis cs:\pgfkeysvalueof{/pgfplots/xmin},0)--
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);

fill opacity=0.05]coordinates {
(1, 1)
(1, 0)
(0, 0)  };
\node [rotate=48] at (axis cs:  .7,  .59) {$y=x^2$};
\node [rotate=90] at (axis cs:  1.05,  .25) {$x=1$};

\addplot[gray!50] fill between[of=A and B, soft clip={domain=0:1},];
\end{axis}
\end{tikzpicture}
\caption{$R=\left\{(x,y) \ | \ 0 \leq y \leq x^2, x\leq 1 \right\}$}
\end{figure}
\end{document}


-