5
$\begingroup$

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!

$\endgroup$
2
  • $\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$ Commented Dec 14, 2018 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$ Commented Dec 15, 2018 at 1:57

2 Answers 2

7
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Thank you! I had tried negative values of Scaled but it didnt do anything! $\endgroup$ Commented Dec 14, 2018 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$ Commented Dec 14, 2018 at 17:26
4
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Very simple! Thanks! $\endgroup$ Commented Dec 14, 2018 at 18:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.