I need to add a rectangle to my plot that includes being behind the ticks but not the label of my graph.

I know with Prologyou can add a rectangle, like with the code:

Plot[x^2, {x, 0, 3}, Prolog -> {Red, Rectangle[Scaled[{0., 0.8}], Scaled[{1, 1}]]}, Frame -> True, FrameLabel -> {"x", "y"}]

enter image description here

But I need the rectangle to go beyond where it is now, to be wider, and actually be behind the number 8 (but not behind the frame label) and go passed the frame a little bit on other side too. With Background I could cover the whole thing but I am only interested in that rectangle and need the rest of the picture to remain backgroundless.

Is it at all posible? Thank you!

  • $\begingroup$ Interestingly, I just got here and had a look at the images. I first saw the images in the two answers and thought, "ugh, this looks ugly with the rectangle extending outside the box". Then I saw the nice image in your question. And finally I was surprised to learn that you actually asked for the style in the answers! $\endgroup$ – Andreas Rejbrand Dec 14 '18 at 23:07
  • $\begingroup$ Yes. The graph I am trying to make is obviously way more complicated than that but that's what I was looking for. It serves to hightlight other graphics that will be displayed along side this one. $\endgroup$ – M.O. Dec 15 '18 at 1:57

You can use PlotRangeClipping->False and extend the Rectangle:

Plot[x^2, {x, 0, 3},
    Prolog -> {Red, Rectangle[Scaled[{-.03,.8}],Scaled[{1.03,1}]]},
    Frame -> True,
    FrameLabel -> {"x","y"},
    PlotRangeClipping -> False

enter image description here

|improve this answer|||||
  • $\begingroup$ Thank you! I had tried negative values of Scaled but it didnt do anything! $\endgroup$ – M.O. Dec 14 '18 at 17:19
  • $\begingroup$ My plot is a ListLinePlot of imported data. The data beyond the range is showing becuase the plotting area hasnt been clipped. Is there any way to hide it that you know of? $\endgroup$ – M.O. Dec 14 '18 at 17:26

Here is alternative way to do it.

Show[Graphics[{Red, Rectangle[{-0.1, 7.8}, {3, 9}]}, 
  PlotRange -> {{0, 3}, {0, 9}}, AspectRatio -> 1/GoldenRatio, 
  Frame -> True, FrameLabel -> {"x", "y"}], Plot[x^2, {x, 0, 3}]]

enter image description here

|improve this answer|||||
  • $\begingroup$ Very simple! Thanks! $\endgroup$ – M.O. Dec 14 '18 at 18:35

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.