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

1
  • If you want to use pgf-plot instead of plain TikZ, you can also follow one of the numerous questions about how to fill under a curve.
    – SebGlav
    Mar 8, 2021 at 17:44

1 Answer 1

0

enter image description hereSomething like this?

\documentclass{article}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \draw[very thin, gray!30, step=1 cm](-4.9,-3.9) 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}
2
  • 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 8, 2021 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 8, 2021 at 18:05

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