Red plot background for negative sign
You can do this with the Filling
option. Change Green
to Opacity[0]
if you don't want it.
f[x_] := Sin[2*Pi*x]
Plot[{f[x], 0}, {x, 0, 2}, Filling -> {1 -> {{2}, {Red, Green}}}]
Edit: to achieve what you want in your edit you can use a Sign
function:
f[x_] := Sin[2*Pi*x]
Plot[{Sign[-f[x]], f[x]}, {x, 0, 2}, Filling -> {1 -> Bottom},
FillingStyle -> Red, PlotStyle -> {{Opacity[0]}, {Thick, Black}}]
Also, if you do this with a function and min/max is outside of {-1,1} then you'll need to scale the Sign
up and limit the plot range because otherwise you'd have it capped off at +1 and a ridge at -1.
f[x_] := 6 E^-x Sin[2*Pi*x^2]
Plot[{1000 Sign[-f[x]], f[x]}, {x, 0, 2}, Filling -> {1 -> Bottom},
FillingStyle -> Red, PlotStyle -> {{Opacity[0]}, {Thick, Black}},
PlotRange -> {-5, 5}]
Plot[{f[x], ConditionalExpression[0, f[x] < 0]}, {x, 0, 2},
PlotStyle -> {Black, None},
Filling -> {2 -> {Bottom, Red}, 2 -> {Top, Red}}]
Plot[{f[x], f[x], ConditionalExpression[0, f[x] < 0]}, {x, 0, 2},
PlotStyle -> {Directive[AbsoluteThickness[5], White], Black, None},
Filling -> {3 -> {Bottom, Red}, 3 -> {Top, Red}}]
f2[x_] := x Sin[x Cos[3 x ]]
Plot[{f2[x], ConditionalExpression[0, f2[x] < 0]}, {x, -2 Pi, 2 Pi},
PlotStyle -> {Black, None}, PlotPoints -> 100,
Filling -> {2 -> {Bottom, Red}, 2 -> {Top, Red}}]
Plot[{f2[x], f2[x], ConditionalExpression[0, f2[x] < 0]}, {x, -2 Pi, 2 Pi},
PlotStyle -> {Directive[AbsoluteThickness[5], White], Black, None},
Filling -> {3 -> {Bottom, Red}, 3 -> {Top, Red}}, PlotPoints -> 100]
Here is a general, but slow method, which uses DensityPlot[]
to generate the shading. To make things more interesting, I'll use a different function:
f[x_] := Exp[(x - 5)/10] BesselJ[0, x] (BesselJZero[0, 3] - x) (x - BesselJZero[0, 4])/20
{xmin, xmax} = {0, 25};
pl = Plot[f[x], {x, xmin, xmax}];
{ymin, ymax} = Last[Charting`get2DPlotRange[pl]];
shade = Cases[DensityPlot[Sign[f[x]], {x, xmin, xmax}, {y, ymin, ymax},
ColorFunction -> (RGBColor[1, 0, 0, 0.8 (1 - #)] &)],
_GraphicsComplex, ∞];
Show[pl, Prolog -> shade]
As you can see, it works even if the function does not exhibit sign changes at its zeroes.
Because that method can be slow, I will also present a less-general procedure, which is only useful if the function is continuous, and all of the its zeroes are of odd multiplicity.
This uses a combination of the MeshFunctions
option of Plot[]
to locate zeroes, and Rectangle[]
+ Scaled[]
to generate the shading. I will again use a different function for the purpose:
f[x_] := AiryAi[x]
pl = Plot[f[x], {x, -14, 2}, Mesh -> {{0}},
MeshFunctions -> {#2 &}, MeshStyle -> Automatic];
np = First[Normal[pl]];
rts = Sort[Cases[np, Point[{x_, _}] :> x, ∞]];
lin = Flatten[Cases[np, Line[l_] :> l, ∞], 1];
{x0, f0} = First[MinimalBy[lin, First]];
{x1, f1} = First[MaximalBy[lin, First]];
sgn = Join[{Sign[f0]}, Sign[f'[rts]], {Sign[f1]}];
rts = Join[{x0}, rts, {x1}];
shade = Apply[Rectangle,
MapThread[Scaled, {{{0, -1}, {0, 1}},
Transpose[{rts[[#]], {0, 0}}]}]] & /@
SequencePosition[sgn, {-1, 1}];
Graphics[{{Opacity[2/3, Red], shade}, DeleteCases[np, _Point, ∞]},
AspectRatio -> 1/GoldenRatio, Axes -> True, PlotRange -> PlotRange[pl]]