Skip to main content
deleted 8 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Plot[Sin[x], {x, -2 Pi, 2 \[Pi]π}, 
   Mesh -> {{0}}, 
   MeshShading -> {Directive@{Thick, Blue}}, Filling -> Axis, 
   ColorFunction -> (If[-Pi <= #1 <= Pi/2, If[#2 > 0, Red, Yellow], White] &),
   ColorFunctionScaling -> False]
Manipulate[Column[{ Dynamic@Show[ Plot[func[x], {x, -2 Pi, 2 \[Pi]π}, 
  Ticks -> {Range[-2 Pi, 2 Pi, Pi/2], Automatic},
  Mesh -> {{0}}, MeshShading -> {Directive@{Thick, color0}},
  Filling -> Axis,
  ColorFunction -> (If[lb <= #1 <= ub, If[#2 > 0, color1, color2], White] &),
  ColorFunctionScaling -> False, ImageSize -> {600, 300}],
  Graphics[{Gray, Line[{{-2 Pi, 0}, {2 Pi, 0}}],
   Orange, PointSize[.02], Dynamic[(Point[{lb = Min[First[pt1], First[pt2]], 0}])],
   Brown, PointSize[.02],  Dynamic[(Point[{ub = Max[First[pt1], First[pt2]], 0}])]},
  PlotRange -> 1.], PlotLabel -> Style[ "\nArea = " <> 
    ToString[Quiet@NIntegrate[func[t], {t, lb, ub}]] <> "\n", 
   "Subsection", GrayLevel[.3]]]},  Center], 
 Row[{Spacer[30], Rotate[Style["functions", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],Control@{{func, Sin, ""},  popupField[#, {Sin, Cos, Sec, Cosh, ArcSinh}] &}
  Spacer[15], Rotate[Style["colors", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],  Rotate[Style["line", GrayLevel[.3], 10], 90 Degree],
  Control@{{color0, Blue, ""}, ColorSlider[#, AppearanceElements -> "Spectrum",
           ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["above", GrayLevel[.3], 10], 90 Degree],
 Control@{{color1, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
          ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["below", GrayLevel[.3], 10], 90 Degree],
 Control@{{color2, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
           ImageSize -> {40, 40}, AutoAction -> True] &}},Spacer[0]],
 {{lb, -Pi}, ControlType -> None},
 {{ub, 3 Pi/2}, ControlType -> None},
 {{pt1, {-Pi, 0}}, Locator, Appearance -> None},
 {{pt2, {3 Pi/2, 0}}, Locator, Appearance -> None},
 Alignment -> Center, ControlPlacement -> Top, AppearanceElements -> Automatic]
Plot[Sin[x], {x, -2 Pi, 2 \[Pi]}, 
   Mesh -> {{0}}, 
   MeshShading -> {Directive@{Thick, Blue}}, Filling -> Axis, 
   ColorFunction -> (If[-Pi <= #1 <= Pi/2, If[#2 > 0, Red, Yellow], White] &),
   ColorFunctionScaling -> False]
Manipulate[Column[{ Dynamic@Show[ Plot[func[x], {x, -2 Pi, 2 \[Pi]}, 
  Ticks -> {Range[-2 Pi, 2 Pi, Pi/2], Automatic},
  Mesh -> {{0}}, MeshShading -> {Directive@{Thick, color0}},
  Filling -> Axis,
  ColorFunction -> (If[lb <= #1 <= ub, If[#2 > 0, color1, color2], White] &),
  ColorFunctionScaling -> False, ImageSize -> {600, 300}],
  Graphics[{Gray, Line[{{-2 Pi, 0}, {2 Pi, 0}}],
   Orange, PointSize[.02], Dynamic[(Point[{lb = Min[First[pt1], First[pt2]], 0}])],
   Brown, PointSize[.02],  Dynamic[(Point[{ub = Max[First[pt1], First[pt2]], 0}])]},
  PlotRange -> 1.], PlotLabel -> Style[ "\nArea = " <> 
    ToString[Quiet@NIntegrate[func[t], {t, lb, ub}]] <> "\n", 
   "Subsection", GrayLevel[.3]]]},  Center], 
 Row[{Spacer[30], Rotate[Style["functions", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],Control@{{func, Sin, ""},  popupField[#, {Sin, Cos, Sec, Cosh, ArcSinh}] &}
  Spacer[15], Rotate[Style["colors", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],  Rotate[Style["line", GrayLevel[.3], 10], 90 Degree],
  Control@{{color0, Blue, ""}, ColorSlider[#, AppearanceElements -> "Spectrum",
           ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["above", GrayLevel[.3], 10], 90 Degree],
 Control@{{color1, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
          ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["below", GrayLevel[.3], 10], 90 Degree],
 Control@{{color2, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
           ImageSize -> {40, 40}, AutoAction -> True] &}},Spacer[0]],
 {{lb, -Pi}, ControlType -> None},
 {{ub, 3 Pi/2}, ControlType -> None},
 {{pt1, {-Pi, 0}}, Locator, Appearance -> None},
 {{pt2, {3 Pi/2, 0}}, Locator, Appearance -> None},
 Alignment -> Center, ControlPlacement -> Top, AppearanceElements -> Automatic]
Plot[Sin[x], {x, -2 Pi, 2 π}, 
   Mesh -> {{0}}, 
   MeshShading -> {Directive@{Thick, Blue}}, Filling -> Axis, 
   ColorFunction -> (If[-Pi <= #1 <= Pi/2, If[#2 > 0, Red, Yellow], White] &),
   ColorFunctionScaling -> False]
Manipulate[Column[{ Dynamic@Show[ Plot[func[x], {x, -2 Pi, 2 π}, 
  Ticks -> {Range[-2 Pi, 2 Pi, Pi/2], Automatic},
  Mesh -> {{0}}, MeshShading -> {Directive@{Thick, color0}},
  Filling -> Axis,
  ColorFunction -> (If[lb <= #1 <= ub, If[#2 > 0, color1, color2], White] &),
  ColorFunctionScaling -> False, ImageSize -> {600, 300}],
  Graphics[{Gray, Line[{{-2 Pi, 0}, {2 Pi, 0}}],
   Orange, PointSize[.02], Dynamic[(Point[{lb = Min[First[pt1], First[pt2]], 0}])],
   Brown, PointSize[.02],  Dynamic[(Point[{ub = Max[First[pt1], First[pt2]], 0}])]},
  PlotRange -> 1.], PlotLabel -> Style[ "\nArea = " <> 
    ToString[Quiet@NIntegrate[func[t], {t, lb, ub}]] <> "\n", 
   "Subsection", GrayLevel[.3]]]},  Center], 
 Row[{Spacer[30], Rotate[Style["functions", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],Control@{{func, Sin, ""},  popupField[#, {Sin, Cos, Sec, Cosh, ArcSinh}] &}
  Spacer[15], Rotate[Style["colors", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],  Rotate[Style["line", GrayLevel[.3], 10], 90 Degree],
  Control@{{color0, Blue, ""}, ColorSlider[#, AppearanceElements -> "Spectrum",
           ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["above", GrayLevel[.3], 10], 90 Degree],
 Control@{{color1, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
          ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["below", GrayLevel[.3], 10], 90 Degree],
 Control@{{color2, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
           ImageSize -> {40, 40}, AutoAction -> True] &}},Spacer[0]],
 {{lb, -Pi}, ControlType -> None},
 {{ub, 3 Pi/2}, ControlType -> None},
 {{pt1, {-Pi, 0}}, Locator, Appearance -> None},
 {{pt2, {3 Pi/2, 0}}, Locator, Appearance -> None},
 Alignment -> Center, ControlPlacement -> Top, AppearanceElements -> Automatic]
added 2970 characters in body; added 116 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Update: Yet another method using ColorFunction with ColorFunctionScaling->False, Mesh and MeshShading,

Plot[Sin[x], {x, -2 Pi, 2 \[Pi]}, 
   Mesh -> {{0}}, 
   MeshShading -> {Directive@{Thick, Blue}}, Filling -> Axis, 
   ColorFunction -> (If[-Pi <= #1 <= Pi/2, If[#2 > 0, Red, Yellow], White] &),
   ColorFunctionScaling -> False]

enter image description here

Update 2: All inside Manipulate:

First, a cool combo control from somewhere in the docs:

 popupField[Dynamic[var_], list_List] :=  
   Grid[{{PopupMenu[Dynamic[var], list, 0, 
          Opener[False, Appearance -> Medium]], 
          InputField[Dynamic[var], Appearance -> "Frameless"]}}, 
  Frame -> All, FrameStyle -> Orange, 
  Background -> {{Orange, Orange}}]

and, then,

Manipulate[Column[{ Dynamic@Show[ Plot[func[x], {x, -2 Pi, 2 \[Pi]}, 
  Ticks -> {Range[-2 Pi, 2 Pi, Pi/2], Automatic},
  Mesh -> {{0}}, MeshShading -> {Directive@{Thick, color0}},
  Filling -> Axis,
  ColorFunction -> (If[lb <= #1 <= ub, If[#2 > 0, color1, color2], White] &),
  ColorFunctionScaling -> False, ImageSize -> {600, 300}],
  Graphics[{Gray, Line[{{-2 Pi, 0}, {2 Pi, 0}}],
   Orange, PointSize[.02], Dynamic[(Point[{lb = Min[First[pt1], First[pt2]], 0}])],
   Brown, PointSize[.02],  Dynamic[(Point[{ub = Max[First[pt1], First[pt2]], 0}])]},
  PlotRange -> 1.], PlotLabel -> Style[ "\nArea = " <> 
    ToString[Quiet@NIntegrate[func[t], {t, lb, ub}]] <> "\n", 
   "Subsection", GrayLevel[.3]]]},  Center], 
 Row[{Spacer[30], Rotate[Style["functions", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],Control@{{func, Sin, ""},  popupField[#, {Sin, Cos, Sec, Cosh, ArcSinh}] &}
  Spacer[15], Rotate[Style["colors", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],  Rotate[Style["line", GrayLevel[.3], 10], 90 Degree],
  Control@{{color0, Blue, ""}, ColorSlider[#, AppearanceElements -> "Spectrum",
           ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["above", GrayLevel[.3], 10], 90 Degree],
 Control@{{color1, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
          ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["below", GrayLevel[.3], 10], 90 Degree],
 Control@{{color2, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
           ImageSize -> {40, 40}, AutoAction -> True] &}},Spacer[0]],
 {{lb, -Pi}, ControlType -> None},
 {{ub, 3 Pi/2}, ControlType -> None},
 {{pt1, {-Pi, 0}}, Locator, Appearance -> None},
 {{pt2, {3 Pi/2, 0}}, Locator, Appearance -> None},
 Alignment -> Center, ControlPlacement -> Top, AppearanceElements -> Automatic]

enter image description here

Enter your own pure function:

enter image description here

Update: Yet another method using ColorFunction with ColorFunctionScaling->False, Mesh and MeshShading,

Plot[Sin[x], {x, -2 Pi, 2 \[Pi]}, 
   Mesh -> {{0}}, 
   MeshShading -> {Directive@{Thick, Blue}}, Filling -> Axis, 
   ColorFunction -> (If[-Pi <= #1 <= Pi/2, If[#2 > 0, Red, Yellow], White] &),
   ColorFunctionScaling -> False]

enter image description here

Update 2: All inside Manipulate:

First, a cool combo control from somewhere in the docs:

 popupField[Dynamic[var_], list_List] :=  
   Grid[{{PopupMenu[Dynamic[var], list, 0, 
          Opener[False, Appearance -> Medium]], 
          InputField[Dynamic[var], Appearance -> "Frameless"]}}, 
  Frame -> All, FrameStyle -> Orange, 
  Background -> {{Orange, Orange}}]

and, then,

Manipulate[Column[{ Dynamic@Show[ Plot[func[x], {x, -2 Pi, 2 \[Pi]}, 
  Ticks -> {Range[-2 Pi, 2 Pi, Pi/2], Automatic},
  Mesh -> {{0}}, MeshShading -> {Directive@{Thick, color0}},
  Filling -> Axis,
  ColorFunction -> (If[lb <= #1 <= ub, If[#2 > 0, color1, color2], White] &),
  ColorFunctionScaling -> False, ImageSize -> {600, 300}],
  Graphics[{Gray, Line[{{-2 Pi, 0}, {2 Pi, 0}}],
   Orange, PointSize[.02], Dynamic[(Point[{lb = Min[First[pt1], First[pt2]], 0}])],
   Brown, PointSize[.02],  Dynamic[(Point[{ub = Max[First[pt1], First[pt2]], 0}])]},
  PlotRange -> 1.], PlotLabel -> Style[ "\nArea = " <> 
    ToString[Quiet@NIntegrate[func[t], {t, lb, ub}]] <> "\n", 
   "Subsection", GrayLevel[.3]]]},  Center], 
 Row[{Spacer[30], Rotate[Style["functions", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],Control@{{func, Sin, ""},  popupField[#, {Sin, Cos, Sec, Cosh, ArcSinh}] &}
  Spacer[15], Rotate[Style["colors", GrayLevel[.3], 12], 90 Degree],
  Spacer[5],  Rotate[Style["line", GrayLevel[.3], 10], 90 Degree],
  Control@{{color0, Blue, ""}, ColorSlider[#, AppearanceElements -> "Spectrum",
           ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["above", GrayLevel[.3], 10], 90 Degree],
 Control@{{color1, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
          ImageSize -> {40, 40}, AutoAction -> True] &},
 Spacer[5], Rotate[Style["below", GrayLevel[.3], 10], 90 Degree],
 Control@{{color2, Green, ""}, ColorSlider[#, AppearanceElements -> "Spectrum", 
           ImageSize -> {40, 40}, AutoAction -> True] &}},Spacer[0]],
 {{lb, -Pi}, ControlType -> None},
 {{ub, 3 Pi/2}, ControlType -> None},
 {{pt1, {-Pi, 0}}, Locator, Appearance -> None},
 {{pt2, {3 Pi/2, 0}}, Locator, Appearance -> None},
 Alignment -> Center, ControlPlacement -> Top, AppearanceElements -> Automatic]

enter image description here

Enter your own pure function:

enter image description here

added 18 characters in body; added 3 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

An alternative is to use PiecewisePiecewise as follows

Plot[{Sin[x], Piecewise[{{Sin[x], -Pi <= x <= Pi}}, _]}, {x, -2 Pi, 2 Pi}, 
  Filling -> {2 -> {Axis, Yellow}}, PlotStyle -> {Green, Directive[Red, Thick]}] 

which gives

enter image description here

Or use

Use ShowShow to superimpose two variants (the second one with your choice of the variable bounds -- -Pi and 2Pi innin the example below) of the plot:

Show[Plot[Sin[x], {x, -3 Pi, 3 Pi}], 
     Plot[Sin[x], {x, - Pi, 2 Pi}, 
         Filling -> Axis, FillingStyle -> Yellow]]

enter image description here

An alternative is to use Piecewise as follows

Plot[{Sin[x], Piecewise[{{Sin[x], -Pi <= x <= Pi}}, _]}, {x, -2 Pi, 2 Pi}, 
  Filling -> {2 -> {Axis, Yellow}}, PlotStyle -> {Green, Directive[Red, Thick]}] 

which gives

enter image description here

Or use Show to superimpose two variants (the second one with your choice of the variable bounds -- -Pi and 2Pi inn the example below) of the plot:

Show[Plot[Sin[x], {x, -3 Pi, 3 Pi}], 
     Plot[Sin[x], {x, - Pi, 2 Pi}, 
         Filling -> Axis, FillingStyle -> Yellow]]

enter image description here

An alternative is to use Piecewise as follows

Plot[{Sin[x], Piecewise[{{Sin[x], -Pi <= x <= Pi}}, _]}, {x, -2 Pi, 2 Pi}, 
  Filling -> {2 -> {Axis, Yellow}}, PlotStyle -> {Green, Directive[Red, Thick]}] 

which gives

enter image description here

Or

Use Show to superimpose two variants (the second one with your choice of the variable bounds -- -Pi and 2Pi in the example below) of the plot:

Show[Plot[Sin[x], {x, -3 Pi, 3 Pi}], 
     Plot[Sin[x], {x, - Pi, 2 Pi}, 
         Filling -> Axis, FillingStyle -> Yellow]]

enter image description here

added 379 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading