3
$\begingroup$

How to fill the area between two curves, but without plotting those curves?

For example,

Plot[Sin[x], {x, 0, 2 Pi}, Filling -> {1 -> 0.5}]

gives a plot like this

enter image description here

(0.5 without any marked boundary = form required for a generalization)

but Plot[Sin[x], {x, 0, 2 Pi}, Filling -> {1 -> (Sin[x] - 0.5)}]

draws a plot without filling or even gives an error

$\endgroup$
2
  • 3
    $\begingroup$ PlotStyle -> None? $\endgroup$
    – Kuba
    Commented Dec 1, 2017 at 12:58
  • $\begingroup$ wow, thank you @Kuba $\endgroup$ Commented Dec 1, 2017 at 13:18

1 Answer 1

6
$\begingroup$

In addition to using PlotStyle->None as suggested by @Kuba, you can also do:

Post-process to remove the Line primitives:

Plot[Sin[x], {x, 0, 2 Pi}, Filling -> {1 -> 0.5}] /. _Line :> Sequence[]

enter image description here

Plot[{Sin[x], Sin[x] - 0.5}, {x, 0, 2 Pi}, Filling -> {1 -> {2}}] /. _Line :> Sequence[] 

enter image description here

Use ParametricPlot with options BoundaryStyle -> None and Mesh -> None:

ParametricPlot[{x, t Sin[x] + (1 - t) .5}, {x, 0, 2 Pi}, {t, 0, 1}, 
 BoundaryStyle -> None, Mesh -> None, AspectRatio -> 1/GoldenRatio, Frame -> False]

enter image description here

ParametricPlot[{x, t Sin[x] + (1 - t) (Sin[x] - 0.5)}, {x, 0, 2 Pi}, {t, 0, 1}, 
 BoundaryStyle -> None, Mesh -> None, AspectRatio -> 1/GoldenRatio, Frame -> False]

enter image description here

$\endgroup$
2
  • $\begingroup$ Would there is a reason for RuleDelayed here /. _Line :> Sequence[] instead of just Rule? $\endgroup$
    – hana
    Commented Mar 9, 2022 at 11:24
  • 1
    $\begingroup$ @hana, no reason (just habitual use of :> when there is a pattern on the LHS) $\endgroup$
    – kglr
    Commented Mar 10, 2022 at 0:11

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.