Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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]
    \addplot[domain=-.15:1.05,blue] {x^2};
        \addplot [thick,color=blue,mark=o,fill=blue, 
                    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} \\

Screen shot of current code

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?

share|improve this question
    
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 at 13:08

4 Answers 4

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

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.10}

\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture} 
    \begin{axis}[enlargelimits=0.1]
    \addplot[name path=f,domain=-.15:1.05,blue] {x^2};

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

    \addplot [
        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:

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.10}

\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture} 
    \begin{axis}[enlargelimits=0.1]
    \addplot[name path=f,domain=-.15:1.05,blue] {x^2};

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

    \addplot [
        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.

share|improve this answer

Use \closedcycle for addplot:

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


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

    \begin{axis}[enlargelimits=0.1]
    \addplot[domain=-.15:1,fill=gray!50] {x^2}\closedcycle;
        \addplot [thick,color=blue,mark=o,fill=blue, 
                    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}

enter image description here

share|improve this answer
    
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 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

enter image description here

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;
\addplot[fill=white,domain=-.15:1.05] {x^2};
\end{scope}
    \addplot[domain=-.15:1.05,blue] {x^2};
    \addplot[thick,color=blue,mark=o,fill=blue, 
                    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}
share|improve this answer

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]
\addplot[domain=-.15:0]{x^2};
\addplot[domain=-0:1,fill=gray!50] {x^2}\closedcycle;

    \addplot [thick,color=blue,mark=o,fill=blue, 
                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]
    \addplot[domain=-0.15:1,name path=A] {x^2};

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

    \addplot [thick,color=blue,mark=o,fill=blue, 
                    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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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