2

So, I have this code and I need to fill area under curve points. How can I do that?

a) First, I need to fill area under curve points from x = 1 to x = 5

b) I need to complete fill area.

Thanks.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
  \begin{tikzpicture}[>=latex]
\centering
\begin{axis}[
  axis x line=center,
  axis y line=center,
  width={\linewidth},
  xtick={1,2,...,8},
  ytick={1,2,...,13},
  xlabel={$x$},
  ylabel={$y$},
  xlabel style={below right},
  ylabel style={above left},
  xmin= 0,
  xmax=8.5,
  ymin=-.5,
  ymax=13.5]

  \addplot+ [mark=*] table {
1  1
2  3
3  5
4  6
5  8
6  10
7  12
8  13
};
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

1
  • Welcome to TeX.SX! There are already quite a few answers to such problems on this site. Look for fill between. Or see this Q&A for example. Commented May 4 at 5:57

1 Answer 1

1

Trivial solution. Changes in yours code:

};
\fill[cyan] (1,1)--(2,3)--(3,5)--(4,6)--(5,8)--(5,0)--(1,0)--cycle;
\pgfmathsetmacro{\A}{.5*((1+3)+(3+5)+(5+6)+(6+8))}
\node[fill=white] at (3,2) {Area=\A};
\end{axis}

Output:

enter image description here

2
  • Interesting. And how can I do if I have a big data .txt, for example? How can I do to read .txt for columns x and y and fill this area? Commented May 4 at 0:19
  • That warrants a separate question, as parsing file contents into LaTeX is a different issue
    – bonk
    Commented May 4 at 4:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .