Graphics, Figures & TablesFill In Graph

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
coachbennett1981
Posts: 274
Joined: Fri Feb 05, 2010 10:15 pm

Fill In Graph

Post by coachbennett1981 »

Hey Everyone,

I have tried FillBetween and am now trying to use intersecting segments from a post I had on a different site. I am trying to create this graph from AP Classroom. I can't get the fill correct. If anyone could help, that would be great. I have tried many different combinations of L1--R2, etc with 2-3 terms, but can't get it right. I have also read the literature, but I just don't understand it. I have included a picture of what I am trying to complete.

Code: Select all

\documentclass[border=5mm]{article}
\usepackage{tikz,asymptote,ragged2e}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
  \usetikzlibrary{arrows.meta}
    \usetikzlibrary{backgrounds}
    \pgfplotsset{compat=newest}
    \usetikzlibrary{calc}
    \usepgfplotslibrary{fillbetween}

\pgfplotsset{
    interpret as polar/.style={
            x filter/.code=\pgfmathparse{cos(rawx)*rawy},
            y filter/.code=\pgfmathparse{sin(rawx)*rawy}
        }
}
\begin{document}
\begin{Center}
\begin{tikzpicture}[>=Triangle]
\begin{axis}[
            xtick distance=1,
            ytick distance=1,
            axis lines=center,
            axis equal image,
            enlargelimits=true,
        ]
    \addplot[name path=A,
            very thick,
            domain=0:180,
            samples=360,
            smooth,
            interpret as polar
        ] 
        (x,{1-2*cos(x)});
    \addplot[name path=B,
            very thick,
            domain=0:360,
            samples=360,
            smooth,
            interpret as polar
        ] (x,{1});
       \path[name path=axis] (axis cs:0,0) -- (axis cs:1,0);
       \begin{scope}[on background layer]
        \fill[blue!30, opacity=0.5, intersection segments={
                of=A and B, sequence={R2 -- L2}
            }] -- cycle;
    \end{scope}
        \node[anchor=center,label=north west:$O$] at (0,0){};  
\end{axis}
\end{tikzpicture}
\end{Center}
\end{document}
Attachments
Screenshot 2025-03-13 at 7.52.31 PM.png
Screenshot 2025-03-13 at 7.52.31 PM.png (44.39 KiB) Viewed 5606 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
User avatar
Stefan Kottwitz
Site Admin
Posts: 10308
Joined: Mon Mar 10, 2008 9:44 pm

Re: Fill In Graph

Post by Stefan Kottwitz »

Hi Nick,

here's a quick idea - fill more with R*--L* and then fill the section with white as you did before:

Code: Select all

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds,arrows.meta}
\pgfplotsset{
    interpret as polar/.style={
            x filter/.code=\pgfmathparse{cos(rawx)*rawy},
            y filter/.code=\pgfmathparse{sin(rawx)*rawy}
        }
}
\begin{document}
\begin{tikzpicture}[>=Triangle]
\begin{axis}[
            xtick distance=1,
            ytick distance=1,
            axis lines=center,
            axis equal image,
            enlargelimits=true,
        ]
    \addplot[name path=A,
            very thick,
            domain=0:180,
            samples=100,
            smooth,
            interpret as polar
        ] 
        (x,{1-2*cos(x)});
    \addplot[name path=B,
            very thick,
            domain=0:360,
            samples=100,
            smooth,
            interpret as polar
        ] (x,{1});
    \addplot[name path=B,
            very thick,
            domain=0:360,
            samples=100,
            smooth,
            interpret as polar
        ] (x,{1});
       \path[name path=axis] (axis cs:0,0) -- (axis cs:1,0);
       \begin{scope}[on background layer]
        \clip (axis cs:0,0) circle (1);
        \fill[blue!30, opacity=0.5, intersection segments={
                of=A and B, sequence={R*--L*}
            }] -- cycle;
        \fill[white, intersection segments={
                of=A and B, sequence={R2 -- L2}
            }] -- cycle;
    \end{scope}
    \node[anchor=center,label=north west:$O$] at (0,0){};
\end{axis}
\end{tikzpicture}
\end{document}
filled-plot.png
filled-plot.png (22.62 KiB) Viewed 5534 times
Stefan
LaTeX.org admin
Post Reply