0

I would like to fill the area between my plot and the x-axis. I tried also the fillbetween library but I cannot figure out exactly how it work.

\documentclass[10pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    style = thick,
    scale only axis=true,
    xmin=-1 ,
    xmax=4 ,
    width=0.8\textwidth,
    height=0.6\textwidth,]
\addplot[thick,
        domain=0:2, 
        samples=100,
        color=blue,
        fill = green,
        fill opacity=0.3] {x^2 - 2*x + 2};
\end{axis}        
\end{tikzpicture}

\end{document}

enter image description here

Fly
  • 315

1 Answers1

0

enter image description hereSomething like this?

\documentclass{article}
\usepackage{tikz}

\begin{document} \begin{tikzpicture} \drawvery thin, gray!30, step=1 cm grid (4.9,3.9);

\fill [green!30, domain=0:2, variable=\x]
  (0, 0)
  -- plot ({\x}, {\x*\x-2*\x+2})
  -- (2, 0)
  -- cycle;

\draw [thick] [->] (-5,0)--(5,0) node[right, below] {$x$};
 \foreach \x in {-4,...,4}
   \draw[xshift=\x cm, thick] (0pt,-1pt)--(0pt,1pt) node[below] {$\x$};

\draw [thick] [->] (0,-4)--(0,4) node[above, left] {$y$};
 \foreach \y in {-3,...,3}
   \draw[yshift=\y cm, thick] (-1pt,0pt)--(1pt,0pt) node[left] {$\y$};

\draw [domain=0:2, variable=\x]
  plot ({\x}, {\x*\x-2*\x+2}) node[right] at (2,2) {$f(x)=(x+1)^2+1$};

\end{tikzpicture} \end{document}

Nick B
  • 831
  • Hello Nick, thank you for your answer. How should I change your solution if, instead of a function, I have a data set from table? – Fly Mar 08 '21 at 17:27
  • unfortunately, I have not really done anything with data sets with LaTeX. I wish I could be of more help. – Nick B Mar 08 '21 at 18:05