# How to color the region between two curves with two different colors?

In the code below, I'm having two problems:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-0.5,0) -- (5.5,0);
\foreach \x in {2,4}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[->,color=black] (0,-0.5) -- (0,4.5);
\foreach \y in {1,2}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-0.5,-0.5) rectangle (5.5,4.5);
\draw [domain=0:5.5] plot(\x,{(\x -2)});
\draw[ smooth,samples=100,domain=0:5.5] plot(\x,{sqrt((\x))});
%\draw [fill=gray,fill opacity=0.3] plot [domain=0:2] (\x,{(\x -2)})--plot [smooth,domain=0:4] (\x,{sqrt((\x))}) ;

\end{tikzpicture}
‎\end{document}

1. Why is the graph near (0,0) not really smooth? 2. How to color the region between two curves like the picture below? ## 1 Answer

### 1. Why is the graph not really smooth?

A pretty straightforward way would be to increase the number of samples. In my answer (cf. below), I defined \sqrtsamples to be the number of samples per unit, which I then multiply by the size of the plotting domain, so you get a uniform plotting resolution.

Edit: Another interesting approach would be to use an implicit plot, so that you can distribute the samples differently. In your case, you'd want denser sampling for small x and coarser sampling for large x. Therefore you could plot over 100 samples of a parameter \t, and choose \x=\t^2. Of course, you then also have to modify the plotting domain:

\draw[smooth,samples=100,domain=0:sqrt(5.5),variable=\t] plot({\t^2},{sqrt(\t^2)});


This will give you a much smoother appearance using way less samples.

### 2. How to color the region between two curves?

You could just use a scope for filling, which you clip to the area between the curves, using the same sampling resolution as for your actual plot (again, cf. example below). Just clip away everything above your sqrt plot and everything below your affine plot and the x-axis. Then fill the left part (up to x=2) of your original clipping area with one colour, and the rest with another. (Edit: My original answer was a bit stupid. You don't actually need to know the intersections of your plots.)

However, here I took into account that you actually want to fill in different colours above and below the specific value x=2. If you want to do it in a more general way, I suggest you have a look at the TikZ library intersections and use the let command to get the x-coordinate of the intersection you're interested in.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

\def\sqrtsamples{100}

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-0.5,0) -- (5.5,0);
\foreach \x in {2,4}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[->,color=black] (0,-0.5) -- (0,4.5);
\foreach \y in {1,2}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-0.5,-0.5) rectangle (5.5,4.5);

% filling scope
\begin{scope}
%% clip to everything between x-axis and sqrt plot
\clip (0,0) -- plot[smooth,samples=5.5*\sqrtsamples,domain=0:5.5](\x,{sqrt((\x))}) -- (5.5,0);
%% clip away everything below affine plot (up to top border of original clipping area)
\clip (0,4.5) -- plot[domain=0:5.5](\x,{(\x -2)}) -- (5.5,4.5);
%% fill areas
\fill[black,opacity=.3] (-0.5,-0.5) rectangle (2,4.5);
\fill[gray,opacity=.3] (2,-0.5) rectangle (5.5,4.5);
\draw[thick] (2,-0.5) -- (2,4.5);
\end{scope}

% plot functions
\draw[domain=0:5.5] plot(\x,{(\x -2)});
\draw[smooth,samples=5.5*\sqrtsamples,domain=0:5.5] plot(\x,{sqrt((\x))});

\end{tikzpicture}
\end{document}


The result: • +1: I took the liberty to add the result of your code. – Claudio Fiandrino Mar 5 '13 at 9:46