4

This question already has an answer here:

Is there a better way to plot a graph and shade a particular area? I've pieced what follows below from other answers, but when I try to extend the axes beyond the plot itself, the whole circle becomes shaded.

\documentclass[15pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\pgfplotsset{width=10cm,compat=1.16}
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{#1}}
\makeatother
\begin{document}    
\begin{center}
        \begin{tikzpicture}
            \begin{axis}[
                data cs=polar,
                smooth,
        axis lines = center,
        xlabel = $x$,
        ylabel = {$y$},]
        \addplot [name path=f, domain=0:360]   {3};
        \path[name path=axis] (axis cs:-3,0) -- (axis cs:3,0);
    \addplot [
            thick,
            color=red,
            fill=red, 
            fill opacity=0.5
        ]
        fill between[
            of=f and axis,
            soft clip={domain=-3:3},
        ];

    \end{axis}
        \end{tikzpicture}

    \end{center}

\end{document}

Thanks!

marked as duplicate by TeXnician, Stefan Pinnow, Kurt, Mike Renfro, Phelype Oleinik May 20 '18 at 18:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SE!. It would be very uselful if you make your code compilable, by adding \documentclass{...} and ending with \end{document}. – Cragfelt May 20 '18 at 7:15
  • I'll add it, but I'm sort of embarrassed by the potentially redundant preamble... – Chris Swan May 21 '18 at 10:02
3

is this what you after for?

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}

\begin{document}
    \begin{center}
\begin{tikzpicture}
    \begin{axis}[
    data cs=polar,
    smooth,
    axis lines = center,
    axis equal,
    xlabel = $x$,
    ylabel = {$y$},
    ymin=-2.5, ymax=2.5
                    ]
    \addplot [name path=f, domain=0:180]    {2}; % <--- changed
    \addplot [domain=180:360]               {2}; % <--- new 
    \path[name path=axis] (-3,0) -- (3,0);
    \addplot [thick,
              fill=red,fill opacity=0.5
              ]
    fill between[
        of=f and axis,
        soft clip={domain=-3:3},
                ];
    \end{axis}
\end{tikzpicture}
    \end{center}
\end{document}

as you define paths' names you require fill between circle and line inside it. you should declare locus above (or below) path inside as is done in mwe above-

Not the answer you're looking for? Browse other questions tagged or ask your own question.