# Coloring intersection of curves

\begin{center}
\begin{tikzpicture}[domain=0:6]
\draw[->] (-1,0) -- (6,0) node[below right] {$x$};
\draw[->] (0,-1) -- (0,6) node[above] {$y$};
\draw[color=Naranja] plot (\x, \x) node[right] {$y = x$};
\draw[color=Rojo][domain=0:1.5] plot (\x, 4*\x) node[right] {$y = x$};
\draw[color=Celeste][domain=0.17:6][samples=500] plot (\x, 1/\x) node[right] {$xy = 1$};
\draw[color=Verde][domain=0.51:6][samples=500] plot (\x, 3/\x) node[right] {$xy = 3$};
\end{tikzpicture}
\end{center}


Which produces:

Now, what I want to do is:

i.e. coloring the intersection of the four curves.

Then, my question is:

How could I do the purple coloring of the second picture?

• You should use the axis environment for such curves. – jak123 Nov 12 '15 at 4:50
• Explain yourself. – francolino Nov 12 '15 at 5:41
• @francolino: jak123 was suggesting that you use the pgfplots package. It's based on TikZ and specializes in producing graphs of functions, and it also has a convineant fillbetween option that comes in handy in such cases. – Tom Bombadil Nov 12 '15 at 9:17

Not elegant, but functional:

\documentclass[10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[domain=0:6]

\fill[violet][domain=0.17:6][samples=500] plot (\x, 1/\x) node[right] {$xy = 1$};
\fill[white][domain=0.51:6][samples=500] plot (\x, 3/\x) node[right] {$xy = 3$};
\fill[white] (0,0)--(6,6)--(6,0)--cycle;
\fill[white] (0,0)--(1.5,6)--(0,6)--cycle;
\draw[->] (-1,0) -- (6,0) node[below right] {$x$};
\draw[->] (0,-1) -- (0,6) node[above] {$y$};
\draw[color=orange] plot (\x, \x) node[right] {$y = x$};
\draw[color=red][domain=0:1.5] plot (\x, 4*\x) node[right] {$y = x$};
\draw[color=blue][domain=0.17:6][samples=500] plot (\x, 1/\x) node[right] {$xy = 1$};
\draw[color=green][domain=0.51:6][samples=500] plot (\x, 3/\x) node[right] {$xy = 3$};

\end{tikzpicture}
\end{center}
\end{document}


You probably should consider using the pgfplots package for such tasks. Here's an example how that could be done. There are many other options like labeling the graphs, adding a legend...

## Code

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[   domain=0:6,
mark=none,
samples=500,
ymax=6,
enlargelimits=false,
]

\addplot[black!70] fill between[of=B and C, soft clip={domain=0.5:0.866}];
\addplot[black!50] fill between[of=C and D, soft clip={domain=0.866:1}];
\addplot[black!30] fill between[of=A and D, soft clip={domain=1:1.732}];
%\addplot[black!20] fill between[of=B and C, soft clip={domain=0.5:0.866}];
\end{axis}
\end{tikzpicture}

\end{document}


## Output

Edit 1: I didn't like that you had to specify the intersections manually in this approach. So I expanded the excellent answer of @Jake in this answer to automate it. You can now use a macro \intersectionX which takes the names of two paths and creates a maro holding the x value of their intersection.

## Code

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}

\makeatletter
\newcommand\transformxdimension[2]{
\pgfmathparse{((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/10^\pgfplots@data@scale@trafo@EXPONENT@x}
\expandafter\xdef\csname#2\endcsname{\pgfmathresult}
}
\newcommand\transformydimension[2]{
\pgfmathparse{((#1/\pgfplots@y@veclength)+\pgfplots@data@scale@trafo@SHIFT@y)/10^\pgfplots@data@scale@trafo@EXPONENT@y}
\expandafter\xdef\csname#2\endcsname{\pgfmathresult}
}
\makeatother

\newcommand{\intersectionX}[3]%
% 1 : name of the macro
% 2,3 : name of the curves
{ \node[name intersections={of=#1 and #2, name=#3}] at (#3-1)
{ \pgfgetlastxy{\macrox}{\macroy} \transformxdimension{\macrox}{#3}};
}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[   domain=0:6,
mark=none,
samples=100,
ymax=6,
enlargelimits=false,
]

\intersectionX{B}{C}{XBC}
\intersectionX{B}{D}{XBD}
\intersectionX{A}{C}{XAC}