1
$\begingroup$

I want to this:

enter image description here

from this picture ,can find that "red" in front of "blue"

My code:

Clear["Global`*"];
(*=== data ===*)
ptdata = {{0, 6}, {0.6, 4}, {1.4, 2.8}};
(*=== define function ===*)
interfun1[x_] := InterpolatingPolynomial[ptdata, x];
interfun2[x_] := interfun1@x - 2;

(*=== plot ===*)
Plot[
 {
  If[x < 1.2, interfun1@x],
  If[x >= 1.2, interfun1@x],
  If[x < 1, interfun2@x],
  If[x >= 1, interfun2@x]
  },
 {x, 0, Pi/2},
 Filling -> {
   2 -> {Top, Directive[Opacity@1, Red]},
   4 -> {Top, Directive[Opacity@0.5, Blue]}
   }
 ]

get this

enter image description here

I use mathematica own tools, I found that the red was covered with blue

enter image description here

By checking the forums, this post might be useful, but I can't make sense of it

How can I deal with this problem?

$\endgroup$
3
  • 1
    $\begingroup$ "change the filling order" - why not change the plotting order, then? Plot[{If[x < 1, interfun2@x], If[x >= 1, interfun2@x], If[x < 1.2, interfun1@x], If[x >= 1.2, interfun1@x]}, {x, 0, Pi/2}, Filling -> {4 -> {Top, Directive[Opacity@1, Red]}, 2 -> {Top, Directive[Opacity@0.5, Blue]}}] $\endgroup$ Jun 14 at 8:55
  • $\begingroup$ @J.M. This method is feasible and feels good. If I insist on changing the filling order,what should I do? $\endgroup$ Jun 14 at 9:03
  • $\begingroup$ Nominally, the filling order is dependent on plotting order; I do not know if it can be decoupled. $\endgroup$ Jun 14 at 15:17

2 Answers 2

3
$\begingroup$
Plot[{
  interfun1[x]
  , interfun2[x]
  , If[x > 1.2, interfun1[x]]
  , If[1 < x <= 1.2, interfun2[x]]
  , If[1.2 < x, interfun2[x], interfun1[x]]
  }
 , {x, 0, 1.5}
 , Exclusions -> Automatic
 , Filling -> {3 -> {Top, Directive[Red]}, 
   4 -> {Top, Directive[Blue]}, 5 -> {1}}
 , FillingStyle -> {Blue}
 , Background -> Nest[Lighter, Blend[{Green, Yellow}], 4]
 ]

enter image description here

$\endgroup$
3
$\begingroup$
plt = Plot[...]
plt /. {a__, x : {__, GraphicsGroup[{__Polygon}]}, b__, 
   y : {__, GraphicsGroup[{__Polygon}]}, c__} :> {a, y, b, x, c}

enter image description here

$\endgroup$
1
  • $\begingroup$ I don't understand the replacement rule at the end, can you explain it briefly? $\endgroup$ Jun 15 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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