Fill area of x^2+y^2>1 and x^2+y^2>4 using patterns and tikzpicture

This is the main "algorithm" for the figure. I am pretty sure you know how to do the rest :)

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[
    every node/.style={fill=white,inner sep=1pt},node distance=.3em,
    >=stealth]
\path[pattern=north west lines,pattern color=blue] (-2.5,-2.5) rectangle (2.5,2.5);
\filldraw[fill=white] (0,0) circle (2);
\path[pattern=north east lines,pattern color=red] (-2.5,-2.5) rectangle (2.5,2.5);
\filldraw[fill=white] (0,0) circle (1);
\draw[->] (-3,0) -- (3,0);
\draw[->] (0,-3) -- (0,3);
\path (0,0) node[below left] {$O$}
    (1,0) node[below right] {$1$}
    (2,0) node[below right] {$2$};
\end{tikzpicture}
\end{document}

enter image description here


If you want to use pgfplots, everything is similar.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
    \begin{axis} [
        axis on top,
        axis lines = center,
        axis equal image,
        xticklabel style = {font=\footnotesize}, % I can't read \tiny fonts :)
        yticklabel style = {font=\footnotesize}, 
        xlabel ={$\Re(z)$},
        xtick={1,2},
        ytick={1,2},
        ylabel ={$\Im(z)$},
        legend pos=outer north east,
        legend style={cells={align=left}},
        legend cell align={left},
        clip=false
    ]
    \path[pattern=north west lines,pattern color=blue] 
        (axis cs: -2,-2) rectangle (axis cs: 2,2);
    \addplot[very thick,red,smooth,domain=0:2*pi,dotted,fill=white] 
        ({2*cos((x) r)},{2*sin((x) r)});
    \path[pattern=north east lines,pattern color=red] 
        (axis cs: -2,-2) rectangle (axis cs: 2,2);
    \addplot[very thick,red,smooth,domain=0:2*pi,dotted,fill=white] 
        ({cos((x) r)},{sin((x) r)});
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

(Sorry for bad rendered images – latexmkrc can't do anything better I think, and the temporary viewer I am working with does not work well with patterns)


With manual legend entries (hmm, we can't add automatically, because we are not plotting them with \addplot, but with tricks. Also, I can't find a better position for the legend XD

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns,matrix}
\begin{document}
\begin{tikzpicture}
    \begin{axis} [
        axis on top,
        axis lines = center,
        axis equal image,
        xticklabel style = {font=\footnotesize}, % I can't read \tiny fonts :)
        yticklabel style = {font=\footnotesize}, 
        xlabel ={$\Re(z)$},
        xtick={1,2},
        ytick={1,2},
        ylabel ={$\Im(z)$},
        legend pos=outer north east,
        legend style={cells={align=left}},
        legend cell align={left},
        clip=false
    ]
    \path[pattern=north west lines,pattern color=blue] 
        (axis cs: -2,-2) rectangle (axis cs: 2,2);
    \addplot[very thick,red,smooth,domain=0:2*pi,dotted,fill=white] 
        ({2*cos((x) r)},{2*sin((x) r)});
    \path[pattern=north east lines,pattern color=red] 
        (axis cs: -2,-2) rectangle (axis cs: 2,2);
    \addplot[very thick,red,smooth,domain=0:2*pi,dotted,fill=white] 
        ({cos((x) r)},{sin((x) r)});
    \matrix[draw,fill=white,ampersand replacement=\&,matrix of math nodes,
            nodes={font=\footnotesize,inner sep=2pt},below right]
    at (axis cs: 2,2) {%
        \path[pattern=north east lines,pattern color=red] (-4pt,0pt) rectangle (4pt,8pt); \&
        x^2+y^2>1\\
        \path[pattern=north west lines,pattern color=blue] (-4pt,0pt) rectangle (4pt,8pt); \&
        x^2+y^2>4\\
    };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here


For completeness: with even odd rule instead of filling with white.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\path[pattern=north west lines,pattern color=blue,even odd rule] (-2.5,-2.5)
rectangle (2.5,2.5) (0,0) circle[radius=2cm];
\path[pattern=north east lines,pattern color=red,even odd rule] (-2.5,-2.5)
rectangle (2.5,2.5) (0,0) circle[radius=1cm];
\draw (0,0) circle[radius=1cm] (0,0) circle[radius=2cm];
\draw[-latex] (-3,0) -- (3,0);
\draw[-latex] (0,-3) -- (0,3);
\end{tikzpicture}
\end{document}

enter image description here

Or pgfplots.

\documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\begin{document}

\begin{center}
    \begin{tikzpicture}
        \begin{axis} [
                axis on top,
                axis lines = center,
                axis equal image,
                xticklabel style = {font=\tiny},
                yticklabel style = {font=\tiny},
                xlabel ={$\Re(z)$},
                xtick={1,2},
                ytick={1,2},
                ylabel ={$\Im(z)$},
                legend pos=outer north east,
                legend style={cells={align=left}},
                legend cell align={left},
                clip=false,enlargelimits=0.1
            ]
            \path[pattern=north west lines,pattern color=blue,even odd rule] 
               (current axis.south west) rectangle (current axis.north east) 
               (0,0) circle(2);
            \path[pattern=north east lines,pattern color=red,even odd rule] 
                (current axis.south west) rectangle (current axis.north east) 
                 (0,0) circle(1);
            \addplot[very thick,red,smooth,domain=0:2*pi,dotted] ({cos((x) r)},{sin((x) r)});
            \addplot[very thick,red,smooth,domain=0:2*pi,dotted] ({2*cos((x) r)},{2*sin((x) r)});
        \end{axis}
    \end{tikzpicture}
\end{center}

\end{document}

enter image description here

Tags:

Fill

Tikz Pgf