0
$\begingroup$

How can I insert Fill in the plot below, such that only the area below 0.5 and below the line is filled?

Plot[-x + 1, {x, 0, 1}]

EDIT: I was hoping the solution to this simpler question would also work for my real problem, which involves the following data, but this is not the case:

How can I implement you solution on this data?

Using Michael's solution combined with comments by void life

Plot[{data  , Min[1/2, #] & data }, {x, 0, 1}, PlotStyle -> {Automatic,None}, Filling -> {1 -> 0, 2 -> {{1}, White}}]

I get:

enter image description here

{0.744318, 0.60038, 0.539471, 0.5542, 0.592916, 0.613237, 0.673014, 0.687572, 0.658548, 0.69416, 0.65248, 0.653574, 0.596889, 0.577856, 0.592349, 0.505633, 0.51403, 0.520655, 0.492696, 0.459513, 0.463524, 0.463181, 0.415386, 0.422303, 0.422072, 0.406222, 0.40379, 0.389445, 0.375038, 0.349756, 0.332508, 0.323173, 0.308245, 0.290745, 0.285053, 0.275322, 0.268835, 0.25514, 0.25281, 0.254935, 0.250978, 0.251613, 0.25431, 0.251915, 0.252345, 0.253374, 0.253928, 0.248666, 0.25169, 0.253212, 0.257687, 0.257374, 0.256379, 0.262966, 0.263945, 0.268633, 0.270197, 0.273909, 0.274298, 0.275465, 0.274321, 0.273316}

enter image description here

$\endgroup$
  • 3
    $\begingroup$ Plot[{-x + 1, Min[1/2, -x + 1]}, {x, 0, 1}, PlotStyle -> {Automatic, None}, Filling -> {2 -> 0}]? $\endgroup$ – Michael E2 Jan 13 '19 at 19:46
  • $\begingroup$ I see the trick. $\endgroup$ – user120911 Jan 13 '19 at 19:47
  • $\begingroup$ What is the problem with using Michael's method with your data? Plot your data and Min[1/2,#]& data in the same way as the example. $\endgroup$ – void life Jan 13 '19 at 21:16
  • $\begingroup$ @void life See my latest edit $\endgroup$ – user120911 Jan 13 '19 at 21:28
  • $\begingroup$ Sorry I meant Min[1/2, #]& /@ data. You need to actually Map the values of data to the function. Secondly, Plot takes a function not just a bunch of data points. You need to use ListLinePlot or similar or create a function that will return values from your data. E.g ListLinePlot[{data, Min[1/2, #] & /@ data}, PlotStyle -> {Automatic, None}, Filling -> {1 -> 0, 2 -> {{1}, White}}] will give you what you are after $\endgroup$ – void life Jan 13 '19 at 21:37
3
$\begingroup$

Possibly also this:

Plot[{-x + 1, Min[1/2, -x + 1]}, {x, 0, 1}, 
 PlotStyle -> {Automatic, None}, 
 Filling -> {1 -> 0, 2 -> {{1}, White}}]

enter image description here

$\endgroup$
  • $\begingroup$ I have edited my question. I set out to provide a simple example, but it turns out my real problem may not be solved so easily using your otherwise instructive example. $\endgroup$ – user120911 Jan 13 '19 at 20:26
0
$\begingroup$
data = {0.744318, 0.60038, 0.539471, 0.5542, 0.592916, 0.613237, 
   0.673014, 0.687572, 0.658548, 0.69416, 0.65248, 0.653574, 0.596889,
    0.577856, 0.592349, 0.505633, 0.51403, 0.520655, 0.492696, 
   0.459513, 0.463524, 0.463181, 0.415386, 0.422303, 0.422072, 
   0.406222, 0.40379, 0.389445, 0.375038, 0.349756, 0.332508, 
   0.323173, 0.308245, 0.290745, 0.285053, 0.275322, 0.268835, 
   0.25514, 0.25281, 0.254935, 0.250978, 0.251613, 0.25431, 0.251915, 
   0.252345, 0.253374, 0.253928, 0.248666, 0.25169, 0.253212, 
   0.257687, 0.257374, 0.256379, 0.262966, 0.263945, 0.268633, 
   0.270197, 0.273909, 0.274298, 0.275465, 0.274321, 0.273316};

threshold = 0.5;

Show[Plot[data, {x, 0, 1}, PlotStyle -> {Automatic, None}], 
 Plot[threshold, {x, 0, 1}, Filling -> Min@data]]

enter image description here

Or

Show[Plot[data, {x, 0, 1}, PlotStyle -> {Automatic, None}, 
  PlotRange -> {0.2, 0.8}], 
 Plot[threshold, {x, 0, 1}, Filling -> 0.2]]

enter image description here

$\endgroup$

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.