# One fill color for a curve when it is above the x-axis and another when it is below [duplicate]

I want to use a different fill for a plot when the curve goes below the x-axis than when it's above that. I tried:

Plot[Sinc[x], {x, 0, 10}, PlotStyle -> Thick, Filling -> 0,
ColorFunction -> Function[{x, y}, If[y < 0, Red, Blue]]]


but then the whole curve has a blue filling. How should I do this?

I expected that this question has been answered before, but I couldn't find it. If it has, my apologies.

It may be instructive to show how your attempt can be corrected. As is often(1)(2),(3),(4),(5),(6)(7)(8) the case when ColorFunction does not behave as anticipated the automatic scaling caused by ColorFunctionScaling defaulting to True is the culprit. If we set that Option to False the raw y values will be used rather than values scaled to the interval (0, 1). For better or worse this method also colors the line of the plot red or blue rather than only the fill.

Plot[Sinc[x], {x, 0, 10},
PlotStyle -> Thick,
Filling -> 0,
ColorFunction -> Function[{x, y}, If[y < 0, Red, Blue]],
ColorFunctionScaling -> False
]


• Thanks for explaining. Still, while it fixes my code, doesn't seem ideal: I can see some blue to the right of x=pi. – flup Dec 28 '13 at 13:49
• @flup That's because of the sampling used; the curve is relatively straight were it crosses the axis, therefore the coloring isn't highly precise. If you use a uniform sampling it will be accurate to within the spacing of the sampling. Try with Options PlotPoints -> 500 and MaxRecursion -> 0 for example. (That's a uniform sampling of 500 points.) – Mr.Wizard Dec 28 '13 at 18:27

Here's an approach without using ColorFunction:

Plot[Sinc[x], {x, 0, 10}, PlotStyle -> Thick, Filling -> {1 -> {0, {Red, Blue}}}]


and I am pretty sure there are lots of similar posts, too...