Shading the area bounded by graphs in TikZ

1,198

Try:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \addplot[name path=f] {x^2 - x +4}; 
    \addplot[name path=g] {x^2 - x}; 
    \addplot [
        fill=blue, 
        fill opacity=0.2
    ]
    fill between[
        of=f and g
    ];
  \end{axis}
\end{tikzpicture}
\end{document}

Output: enter image description here

Share:
1,198
An Hoa
Author by

An Hoa

Updated on August 01, 2022

Comments

  • An Hoa
    An Hoa over 1 year

    Thanks to Torbjørn T.'s answer at Best way to generate a nice function plots in LaTeX?, there is an easy way to show multiple graphs using TikZ's datavisualization. Is there a way to shade i.e. fill in the region bounded by these graphs?

    • John Kormylo
      John Kormylo almost 7 years
      An MWE of the two functions on one axis would be useful.
    • Torbjørn T.
      Torbjørn T. almost 7 years
      As an aside, if you're not locked to the datavisualization library, you could look at pgfplots. Search the site for e.g. [pgfplots] fill between
  • An Hoa
    An Hoa almost 7 years
    Is there a way to do it with datavisualization?
  • Hood Chatham
    Hood Chatham almost 7 years
    Sorry, didn't read that part of your question.