# How do I shade various regions defined by a hobby curve?

Use the following code, I am able to generate the curve easily.

\documentclass{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}[scale=1.0,>=latex, use Hobby shortcut]
\draw[-] (0,3) .. (3,0) .. (4,-2);
\draw[-] (4,-2) .. (5,0) .. (6,1);
\draw[-] (6,1) -- (7,1);
\draw[-] (7,1) .. (8,0) .. (11,-2);
\draw[->,thick] (-.25,0) -- (12,0) node[above] {\footnotesize $x$};
\draw[->,thick] (0,-3) -- (0,4) node[below right]{\footnotesize $f(x)$};
\node[below] at (3,0) {\footnotesize $a$};
\node[below] at (5,0) {\footnotesize $b$};
\node[below] at (8,0) {\footnotesize $c$};
\node[below] at (11,0) {\footnotesize $d$};
\end{tikzpicture}
\end{document}


My problem is I need to shade the regions bounded by f(x) and the x-axis over [0,b], [a,b], [b,c], and [c,d]. How do I achieve that with, say, red for the regions above the x-axis, and blue for the region below?

-

This can be done by means of the fillbetween library which has been written for pgfplots.

\documentclass{standalone}
\usepackage{pgf,tikz}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\usetikzlibrary{arrows}
\usetikzlibrary{hobby}
\begin{document}
\pgfdeclarelayer{pre main}
\begin{tikzpicture}[scale=1.0,>=latex, use Hobby shortcut]
\pgfsetlayers{pre main,main}
\draw[name path=f,-] (0,3) .. (3,0) .. (4,-2)
-- (4,-2) .. (5,0) .. (6,1)
-- (6,1) -- (7,1)
-- (7,1) .. (8,0) .. (11,-2);
\draw[->,thick] (-.25,0) -- (12,0) node[above] {\footnotesize $x$};
\draw[->,thick] (0,-3) -- (0,4) node[below right]{\footnotesize $f(x)$};
\path[name path=xaxis] (0,0) -- (11,0);

\node[below] at (3,0) {\footnotesize $a$};
\node[below] at (5,0) {\footnotesize $b$};
\node[below] at (8,0) {\footnotesize $c$};
\node[below] at (11,0) {\footnotesize $d$};

\tikzfillbetween[
of=f and xaxis,
split,
every even segment/.style={orange},
every segment no 2/.style={top color=white,bottom color=orange},
]{red}

\end{tikzpicture}
\end{document}


Note that I concatenated your path segments to a single path in a trivial way. Then, I assigned name path=f to your path and generated a path for the x axis. Finally, I used \tikzfillbetween in order to compute + fill 4 paths, one for each of the intervals. The option(s) in curly braces ({red}) are applied to every path, the options inside of the square brackets determine how the paths are to be computed. In this case, path segments should be splitted and every second segment should be orange, and the one with index 2 should be shaded (note that the first has index 0).

The layering instructions \pgfdeclarelayer{pre main} and \pgfsetlayers{pre main,main} are merely helpful to place the filled areas behind the plot as such (the fill between layer has in initially configured as on layer=pre main).

Note that you need to load \usepackage{pgfplots} for this solution and you need pgfplots version 1.10. Details about the fillbetween library can be found in the pgfplots manual (compare Section Libraries - Fill between in http://mirrors.ctan.org/graphics/pgf/contrib/pgfplots/doc/pgfplots.pdf)

-
This is great, thanks! I do have a question, though. Why is the (appearance of) color in [0,a] different from that of [b,c]? How can they be made the same? –  DJJerome May 2 at 1:20
It is different because I configured every segment no 2/.style={top color=white,bottom color=orange}. Since indexing starts at 0, [b,c] is the segment with index 2. Uncommenting that item and/or more of the style options around it will make them the same. –  Christian Feuersänger May 2 at 9:34
I guess I wasn't terribly clear in my comment. It's not that I want the same color above and below, but the shading of the orange in the regions above isn't consistent. Did you do that intentionally? –  DJJerome May 2 at 18:50
Well, the shading was chosen intentionally, and the fact that [0,a] has no shading at all was also fully intentional. Only the third segment has a gradient at all... or did you mean something else with "consistent" (if so: consistent with what?). The first segment receives the style every even segment - and only that style - which states that it should be filled with orange (solid). –  Christian Feuersänger May 2 at 19:51