2
$\begingroup$

How do I fill the space between a Cos curve and a horizontal Line Linking to points on the curve? Say, I would like fill the space between a Line[{{Pi/4, Cos[Pi/4]}, {-Pi/4, Cos[Pi/4]}}] the curve Cos[x], {x, -Pi/4, Pi/4}.

$\endgroup$
1

2 Answers 2

4
$\begingroup$

Your Line has a constant $y$ value... so I picked a bit more adventurous example:

With[{line =
   Interpolation[
     {{-Pi, Cos[-Pi]}, {Pi/4, Cos[Pi/4]}},
     InterpolationOrder -> 1]},
 Plot[{Cos[x], Quiet[line[x]]}, {x, -Pi, Pi/4}, Filling -> {1 -> {2}}]]

enter image description here

Line coordinates are fed to first-order Interpolation which creates a function corresponding its $y$ value. This is used as the second equation on Plot. With Filling one can specify the interval to fill (and with FillingStyle one can modify the appearance).

(Quiet is used to silence possible warnings on extrapolation of the resulting InterpolationFunction.)

$\endgroup$
2
$\begingroup$

Try this:

Plot[{Cos[x], Cos[\[Pi]/4]}, {x, -\[Pi]/4, \[Pi]/4}, 
 PlotRange -> {0, 1}, Filling -> {1 -> {2}}]

with the following effect:

enter image description here

Have fun!

$\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.