3
$\begingroup$
f[x_] = If[x >= 0 && x <= 2, {4 - x^2, 2 x - x^2}];

p1 = Plot[f[x], {x, -3, 3}, Frame -> True, GridLines -> Automatic,
   PlotStyle -> Blue, ImageSize -> 550, Filling -> {1 -> {2, Orange}},
    FillingStyle -> Directive[Opacity[0.1], Orange]];
p2 = Plot[{4 - x^2, 2 x - x^2}, {x, -3, 3}]; Show[{p1, p2}, 
 PlotRange -> {-2, 5}]

Why do I get the area to the right colored?

$\endgroup$
1
  • $\begingroup$ f is a vector-valued function, not the scalar function. $\endgroup$
    – cvgmt
    Commented Mar 10, 2023 at 11:52

2 Answers 2

5
$\begingroup$

Even though you return a list in your f[x], Mathematica sees this as a single curve. You should explicitly provide two separate curves:

curves = {4 - x^2, 2 x - x^2};

p1 = Plot[curves, {x, -3, 3}];
p2 = Plot[curves, {x, 0, 2}, 
   Filling -> {1 -> {{2}, Opacity[.2, Orange]}}];

Show[p1, p2, Frame -> True, GridLines -> Automatic]

Plot

$\endgroup$
4
$\begingroup$

You can use Region functionality as follows:

Clear["Global`*"]
curves = {4 - x^2, 2 x - x^2};
p3 = Plot[curves, {x, -3, 3}
  , Frame -> True
  , PlotStyle -> Thick
  , GridLines -> Automatic
  , ImageSize -> 550
  ];
reg = ImplicitRegion[
  y < First@curves && y >= Last@curves, {{x, 0, 2}, y}]
p4 = RegionPlot[reg
   , PlotStyle -> Directive[Yellow, Opacity[0.4]]
   , BoundaryStyle -> Directive[Red, Thick]
   ];

Show[p3, p4]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.