11

I am trying to achieve the following picture using tikzpicture environment:

What I want

The inner circle is x^2+y^2=1 and the other is x^2+y^2=4.

What I have tried:

\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}

\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
            ]
            \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}

What I have tried

I want to add pattern lines for x^2+y^2>1 and x^2+y^2>4, but I do not know how to do it.

As a help you can rely on this example that I made a while ago:

\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.8}
\usetikzlibrary{arrows.meta, patterns}

\begin{document}

    \begin{center}
        \begin{tikzpicture}
            \pgfplotsset{ticks=none}
            \begin{axis}[
                axis equal image,
            axis lines = center,
            xlabel = $x$,
            ylabel = $y$,
                xmax=3,
                xmin=-3,
                legend pos=outer north east,
                axis background/.style={%
          preaction={
           path picture={
                        \draw[pattern=north west lines, pattern color=red,draw=white] (axis cs:1,-3) rectangle (axis cs:3,3);
                        \draw[pattern=north east lines, pattern color=blue,draw=white] (axis cs:1,3) rectangle (axis cs:-3,-3);
           }}},
                xtick={1,2}
                ]
                \node at (axis cs: 2,2.6) {$x^2+y$};
                \node at (axis cs: -2,2.6) {$3x-y$};
                \addplot[very thick, red] coordinates {(1,-3) (1,3)};
            \end{axis}
        \end{tikzpicture}
    \end{center}

\end{document}

Example

11

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

  • Very nice, thank you!! It would be possible to draw the same but using the pgfplots axis like my first code block? – manooooh May 20 at 12:33
  • 1
    @manooooh Legend entry added. – user156344 May 20 at 13:46
7

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

  • 1
    My compliments to you and JouleV for your answer and for the question. Very nice the patterns. – Sebastiano May 20 at 12:17
  • Thanks! It would be possible to have a coordinate of system axis i.e. replicating \begin{axis} as my first picture? – manooooh May 20 at 12:29
  • 1
    @manooooh Do you want to add this to a pgfplots axis or use "plain" TikZ? – marmot May 20 at 12:30
  • The same syntax as my first source code. I want to add this to a pgfplits axis. – manooooh May 20 at 12:32
  • 1
    @manooooh Done/ – marmot May 20 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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