6
$\begingroup$

How can I fill the region between two horizontal lines and two curves like the following?

plot with hand drawn sketch of result

Plot[{Sqrt[(b^2 - 1)/b^2], -Sqrt[(b^2 - 1)/b^2]}, {b, -4, 4}, 
PlotStyle -> ColorData[97][1], GridLines -> {{}, {1, -1}}, 
GridLinesStyle -> {Thick, ColorData[97][1]}]
$\endgroup$
1
  • $\begingroup$ See here for a related discussion, but it is a few years old. $\endgroup$
    – user293787
    Commented Jul 7, 2022 at 9:23

3 Answers 3

6
$\begingroup$

Try this:

Show[{
  Plot[{1, -1}, {b, -4, 4}, PlotStyle -> ColorData[97][1], 
   Filling -> {1 -> {2}}],
  Plot[{Sqrt[(b^2 - 1)/b^2], -Sqrt[(b^2 - 1)/b^2]}, {b, -4, 4}, 
   PlotStyle -> ColorData[97][1], Filling -> {1 -> {2}}, 
   FillingStyle -> White]
  }]

enter image description here

Have fun!

$\endgroup$
4
$\begingroup$

Edit

Clear[plot, pts];
 plot = 
 Plot[{Sqrt[(b^2 - 1)/b^2], -Sqrt[(b^2 - 1)/b^2]}, {b, -4, 4}, 
  PlotPoints -> 50, MaxRecursion -> 2];
pts = Cases[plot, Line[pts_] :> pts, Infinity];
(*Graphics[{Polygon[Join[pts[[1]],pts[[2]]]],Polygon[Join[pts[[3]],\
pts[[4]]]]}]*)
Graphics[{EdgeForm[Blue], FaceForm[Green], 
  Polygon@Join[pts[[1]], Reverse@pts[[3]], Reverse@pts[[4]], 
    pts[[2]]]}, Axes -> True, AspectRatio -> 1/GoldenRatio]

enter image description here

Plot[{Sqrt[(b^2 - 1)/b^2], -Sqrt[(b^2 - 1)/b^2]}, {b, -4, 4}, 
  MaxRecursion -> 10] /. {___, Line[pts1_], 
   Line[pts2_]} -> {FaceForm@Green, Polygon@Join[pts1, pts2]}
ContourPlot[
  x^2 - y^2 == 1, {x, -2, 2}, {y, -2, 2}] /. {__, Line[pts1_], 
   Line[pts2_]} :> {FaceForm@Green, EdgeForm@Blue, 
   Polygon@Join[pts1, pts2]}

Original

Use RegionDifference to get the complement of Filling -> {1 -> {2}.

plot0 = Plot[{-Sqrt[(b^2 - 1)/b^2], Sqrt[(b^2 - 1)/b^2]}, {b, -4, 4}, 
   AspectRatio -> 1, Filling -> {1 -> {2}}]

reg = RegionDifference[Rectangle[{-4, -1}, {4, 1}], 
   DiscretizeGraphics[plot0]];
plot = Plot[{Sqrt[(b^2 - 1)/b^2], -Sqrt[(b^2 - 1)/b^2]}, {b, -4, 4}, 
   PlotStyle -> ColorData[97][1]];
Show[plot, Graphics[{Orange, reg}]]

enter image description here enter image description here

$\endgroup$
3
$\begingroup$

This sets the background to LightBlue and then fills everything but the center region white.

Plot[{Sqrt[(b^2 - 1)/b^2], -Sqrt[(b^2 - 1)/b^2],1,-1}, {b, -4.08, 4.08}, PlotStyle -> ColorData[97][1],Background->LightBlue,Filling->{1->{{2},White},3->{Top,White},4->{Bottom,White}},PlotRangePadding->None,ImagePadding->None,PlotRange->{-1.08,1.08}]

enter image description here

$\endgroup$

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.