2
$\begingroup$

I want to fill the frame background of a Plot with a color for which I am using a Rectangle as a Prolog item.

p1 = Plot[{Sin[2 x], Sin[x]}
  , {x, 0, 2 \[Pi]}
  , Frame -> {{True, True}, {True, True}}
  , FrameTicks -> Automatic
  , Prolog -> {
    Nest[Lighter, Brown, 5]
    , Rectangle[{0, -1.0}, {2 \[Pi], 1.0}]
    }
  ]

enter image description here


Using AbsoluteOptions gives the same values that have been used in the Prolog section above, but it leaves a small margin.

AbsoluteOptions[#, PlotRange] & @p1

{PlotRange -> {{0., 6.28319}, {-1., 1.}}}


EDIT-1

I know PlotRangePadding->None would get me there, but I want to find the coordinates of the frame box.


Question(s)

  1. How do I determine the frame bounding box coordinates inside the Prolog section so that I can put a Rectangle there to fill the frame with a specified color.

  2. Are there built-in options (or other available solutions) for this purpose?

Thanks in advance for your help and suggestions.

$\endgroup$
1
  • 1
    $\begingroup$ AbsoluteOptions has been heavily upgraded and improved in V13. If you evaluate AbsoluteOptions[p1, PlotRangePadding] in V12, you get measurements in the form of Scaled[]. In V13, however, you get PlotRangePadding -> {{0.1309, 0.1309}, {0.111111, 0.111111}}. You can subtract this from coordinates (and add to size of the rectangle) and you will be able to fill the whole area. To do this, I would first plot and use Epilog -> a, then get the AbsoluteOptions and replace p1 /. a -> {Nest ...}. $\endgroup$
    – Domen
    Commented Jan 3, 2022 at 10:53

1 Answer 1

3
$\begingroup$

You can use Scaled and avoid the need for explicit coordinates:

Plot[{Sin[2 x], Sin[x]}, {x, 0, 2 π}, 
 Frame -> True, 
 Prolog -> {Nest[Lighter, Brown, 5], 
   Rectangle[Scaled[{-1, 1}], Scaled[{1, -1}]]}]

enter image description here

Update: In case you need the plot range for other purposes, you can use undocumented function Charting`get2DPlotRange:

 Plot[{Sin[2 x], Sin[x]}, {x, 0, 2 π}, Frame -> True, 
  DisplayFunction ->
     (Show[#, 
       Prolog -> {Nest[Lighter, Brown, 5], 
           Rectangle @@ Transpose[Charting`get2DPlotRange @ #]}] &)]

enter image description here

$\endgroup$
5
  • 2
    $\begingroup$ I'd expect that you should be able to use Rectangle[Scaled[{-1, 1}], Scaled[{1, -1}]] regardless of the version. $\endgroup$ Commented Jan 3, 2022 at 18:06
  • 1
    $\begingroup$ Thank you @Brett. I somehow got some warning message containing ".. Absolute..." first time I used Rectangle[Scaled[{-1, 1}], Scaled[{1, -1}]] in version 13.0 (on Wolfram Cloud) and the message went away when I switched to Rectangle[Scaled[{-1, 1},{0,0}], Scaled[{1, -1},{0,0}]]. (The message was probably triggered by some other option as I cannot reproduce it now). $\endgroup$
    – kglr
    Commented Jan 3, 2022 at 18:11
  • 1
    $\begingroup$ Rectangle[Scaled[{0, 0}], Scaled[{1, 1}] also fills the frame: Can you please explain the -1s in the Scaled as docs mention "... coordinates scaled to run from 0 to 1 across the whole plot range in each direction". Thanks for the answer. $\endgroup$
    – Syed
    Commented Jan 3, 2022 at 18:41
  • 1
    $\begingroup$ Yes, {0,0} and {1,1} are what I would normally use here. $\endgroup$ Commented Jan 3, 2022 at 18:47
  • $\begingroup$ @Syed, as Brett commented, Rectangle[Scaled[{0, 0}], Scaled[{1, 1}] is the better alternative here. (Although they also work, the alternative forms Rectangle[Scaled[{-1,1}], Scaled[{1, -1}], Rectangle[Scaled[{-1,-1}], Scaled[{1, 1}] etc are based on my having used to the two-argument form of Scaled) $\endgroup$
    – kglr
    Commented Jan 3, 2022 at 19:03

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.