Tell me more ×
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.

How to accomplish fill between two curves in pgfplots? For example, I have

...
\addplot coordinates {(0,1) (1,1)};
\addplot file {something.dat};
...

and I would like to have filling between those curves.

share|improve this question

1 Answer

up vote 21 down vote accepted
+50

You can use the \pgfplotstablevertcat command to add the coordinates to your data and then just use the fill option when plotting:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}

% Load the data
\pgfplotstableread{data.dat}\data

% Input the coordinates for the fill boundary, sort them from highest to lowest x value
\pgfplotstablesort[sort cmp={float >}]\coordinates{
0 0
1 1
}

% Concatenate the two series
\pgfplotstablevertcat{\filledcurve}{\data}
\pgfplotstablevertcat{\filledcurve}{\coordinates}


\begin{axis}
% Plot the concatenated tables
\addplot[fill=gray!40,draw=none] table {\filledcurve};

% Plot the data and the coordinates for reference
\addplot[blue,mark=*,line width=2pt] table {\data};
\addplot[orange,mark=*,line width=2pt] table {\coordinates};
\end{axis}
\end{tikzpicture}

\end{document}

My datafile "data.dat" looks like this

0 2
0.5 -1
1 3

Yielding the following output:

Filling the area between two curves using pgfplots

share|improve this answer
Thanks for your answer! – user1996 Jan 6 '11 at 9:56

Your Answer

 
discard

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