# Change the Filling order

I want to this: 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[[email protected], Blue]}
}
]


get this I use mathematica own tools, I found that the red was covered with blue By checking the forums, this post might be useful, but I can't make sense of it

How can I deal with this problem?

• "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[[email protected], Blue]}}] Jun 14, 2022 at 8:55
• @J.M. This method is feasible and feels good. If I insist on changing the filling order，what should I do？ Jun 14, 2022 at 9:03
• Nominally, the filling order is dependent on plotting order; I do not know if it can be decoupled. Jun 14, 2022 at 15:17

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]
] plt = Plot[...]
plt /. {a__, x : {__, GraphicsGroup[{__Polygon}]}, b__,
y : {__, GraphicsGroup[{__Polygon}]}, c__} :> {a, y, b, x, c}
` • I don't understand the replacement rule at the end, can you explain it briefly? Jun 15, 2022 at 8:01