# Pgfplots: how do I fill an area over a curve?

I'm working with pgfplots, and I have a curve on a graph which is basically a rect. I would like to color the part on top of it, not the part under it. I have this code:

\addplot[fill=\sixthplotcolor, fill opacity=0.5, thick, mark=none, color=\sixthplotcolor]
coordinates {
(1, 40)
(10, 400)
(100, 4000)
(150, 6000)
(200, 8000)
(250, 10000)
(300, 12000)
(350, 14000)
(500, 20000)
(750, 30000)
(1000, 40000)
} \closedcycle;


Which colors under the curve, but not over.

Can anyone help me? Sorry for the dumb question, I'm new to pgfplots.

-
For this specific example finish of your plot command with -- (axis cs:0,40000) \closedcycle; and it should fill the upper part. –  percusse Jun 14 '12 at 15:32
@percusse: Or just -| (current plot begin);. –  Jake Jun 14 '12 at 15:33
@Jake Ah, sure. Much better. –  percusse Jun 14 '12 at 15:34
Well, thanks! What does -- (axis cs:0,40000) and -| (current plot begin); do exactly? –  Madhatter Jun 14 '12 at 15:35
Welcome to TeX.SE. Good first question. For future reference, please keep in mind that while code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Jun 14 '12 at 19:24

Version 1.10 of pgfplots has been released just recently, and it comes with a new solution for the problem to fill the area between plots.

For your simple case, the answer of percusse is the way to go.

However, for anything which is slightly more complex, the following approach might be good to know: it allows to fill the area between any two plots by means of the fillbetween library. In order to keep the knowledge base of this site up-to-date, I present a solution based on the new fillbetween library here:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[name path=f, fill=red, fill opacity=0.5, thick, mark=none]
coordinates {
(1, 40)
(10, 400)
(100, 4000)
(150, 6000)
(200, 8000)
(250, 10000)
(300, 12000)
(350, 14000)
(500, 20000)
(750, 30000)
(1000, 40000)
};

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

\end{axis}
\end{tikzpicture}
\end{document}


This solution relies on \usepgfplotslibrary{fillbetween}: it assigns name path=f to the function of interest. Then, it generates an artificial \path which is neither drawn nor filled: this path is at y=40000. Finally, it fills the area between f and axis by means of \addplot fill between.

-

As given in the comments, for this specific example using a predefined special coordinate and orthogonal lines can be used to achieve the goal.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
coordinates {
(1, 40)
(10, 400)
(100, 4000)
(150, 6000)
(200, 8000)
(250, 10000)
(300, 12000)
(350, 14000)
(500, 20000)
(750, 30000)
(1000, 40000)
} -| (current plot begin);
\end{axis}
\end{tikzpicture}
\end{document}


-