5
$\begingroup$

How to use Fill but only under the line which has the lowest value on plot? Consider following code:

data = Table[#, {x,-5,10,1}]&/@{{x, 2 x + 3}, {x, 5 x + 2}, {x, -3 x + 20}, {x, 5}}
ListLinePlot[data]

Which gives a plot:

enter image description here

And that's how it should look:

enter image description here

Since the plots will not always be the same, the bottom lines are changing so I can't just hardcode it

Thank You in advance!

$\endgroup$
0

1 Answer 1

7
$\begingroup$
xylst = {{x, 2 x + 3}, {x, 5 x + 2}, {x, -3 x + 20}, {x, 5}};

data = Table[#, {x, -5, 10, 1}] & /@ xylst;

ylst = xylst[[All, -1]];

celst = Table[ConditionalExpression[f, f == Min[ylst]], {f, ylst}];

Show[ListLinePlot @ data, 
    Plot[celst, {x, -5, 10}, Filling -> Bottom]]

enter image description here

Alternatively, use ylst and celst with Plot:

Plot[Evaluate @ Join[ylst, celst], {x, -5, 10}, 
 PlotStyle -> ColorData[97] /@ Range[4], 
 Filling -> Thread[Range[5, 8] -> Bottom]]

enter image description here

Update: If we have access only to data (not the functions used to generate data):

iFlst = Interpolation /@ data;

cElst = Table[ConditionalExpression[f[x], 
    f[x] == Min[Through@iFlst@x]], {f, iFlst}];

Plot[Evaluate @ Join[Through @ iFlst @ x, cElst], 
 {x, Min @ data[[All, All, 1]], Max @ data[[All, All, 1]]}, 
 PlotStyle -> (ColorData[97] /@ Range[Length @ iFlst]),
 Filling -> Thread[Length @ iFlst + Range[Length @ iFlst] -> Bottom]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Great answer! Works perfect. Thank you! $\endgroup$
    – Lechuu
    Apr 27, 2023 at 9:32

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.