3

I want to draw the following image in tikZ

enter image description here

Basically I want to draw the boundary of the unit square, plot the x^2 and \sqrt x functions, and shade the area between them.

In my attempt to draw the two curves, I tried this

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\pgfplotsset{ticks=none}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,]
\addplot[name path=F,black,domain={0:5}] {sqrt x};
\addplot[name path=G,black,domain={0:5}] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

But the output I am getting is

enter image description here

Somehow the \sqrt{x} function is getting flattened out.

Also, how to get rid of the arrows I have in the axes and make an enclosing box like I have in the hand-drawn picture.

  • Note that the intersection of curves happens for x=1. So change domain to better visualize. – Sigur Jun 23 '18 at 14:17
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – dexteritas Jun 23 '18 at 14:18
  • sqrt x should propbably be sqrt(x) – user121799 Jun 23 '18 at 14:27
  • @marmot That does not make any difference. – dexteritas Jun 23 '18 at 14:33
  • 1
    @dexteritas I did not know about the etiquette you mentioned. But it makes a lot of sense. Thanks. I have edited my post. – caffeinemachine Jun 23 '18 at 14:35
7

Adaptions:

  • Set min=0,xmax=1,ymin=0,ymax=1 to get the desired area of the plots.
  • Remove axis lines=middle to get default axis (rectangle around plots).
  • Use \addplot [fill=blue!10] fill between[of=F and G]; to fill area between plots F and G.

Code:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\pgfplotsset{ticks=none}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xmin=0,xmax=1,
    ymin=0,ymax=1,
    samples=500,
]
    \addplot[name path=F,black,domain={0:1}] {sqrt x};
    \addplot[name path=G,black,domain={0:1}] {x^2};
    \addplot [fill=blue!10] fill between[of=F and G];
\end{axis}
\end{tikzpicture}

\end{document}

Result:

enter image description here

5
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\begin{document}
\pgfplotsset{ticks=none}

\begin{tikzpicture}
\begin{axis}[axis lines=middle,]
\addplot[name path=F,black,domain={0:1},samples=50] {sqrt(x)};
\addplot[name path=G,black,domain={0:1},samples=50] {x^2};
\addplot[pattern=vertical lines] fill between [of=F and G];
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.