4
$\begingroup$

I will give an example of a regular Plot, but I also need this to work for a ParametricPlot. Say I have

plt1=Plot[Sin[x],{x,0,2π}]

I now want to add two vertical lines, one at x=3π/4 abd on at x=5π/4. In addition, I want to shade the region between those two lines. My initial idea was to use ParametricPlot to draw the line, and I hoped it would have a Fillingoption similar to that of Plot, but it does not.

The next idea was to use

plt2=RegionPlot[3π/4<x<5π/4,{x,0,2π},{y,-1.5,1.5}]

where I deliberately draw the box a bit larger on the y-coordinate, since I want it to be clipped at the PlotRange of plt1. Now I thought I combine both plots by

Show[plt1,plt2]

which gives

enter image description here

And this es exactly as I want it, with the one exception that

  1. the shaded region contains that weird mesh of thin lines.

This problem disappears if I show them in opposite order

Show[plt2, plt1]

which gives

enter image description here

But it also introduces the new problems that

  1. the plot range would have to be manually adjusted
  2. the plot is now shown with these boxed axes style, and I am not sure how to change it to the usual one of Plot.

Problem 2. is admittedly the lesser problem. But if someone could help me fix preferably 1. or otherwise 3. that would be great! Thanks!

$\endgroup$
2
  • $\begingroup$ Does this answer your question? Filling complex areas of a ListPlot $\endgroup$
    – C. E.
    Jul 17, 2020 at 15:37
  • $\begingroup$ @C.E. I Suppose it would work, thanks! The answer of Suba Thomas below might be even simpler. $\endgroup$
    – Britzel
    Jul 17, 2020 at 15:50

1 Answer 1

4
$\begingroup$

Prolog is useful here.

Plot[Sin[x], {x, 0, 2 π}, 
 Prolog -> {LightBlue, Rectangle[{3 π/4, -2}, {5 π/4, 2}]}]

enter image description here

$\endgroup$
4
  • $\begingroup$ Nice and simple solution! Thanks a lot! $\endgroup$
    – Britzel
    Jul 17, 2020 at 15:49
  • $\begingroup$ If you want to pimp the box in your answer a bit, use: Prolog -> {Opacity[.2], RGBColor[0.368417, 0.506779, 0.709798], EdgeForm[Directive[Thin, RGBColor[0.368417, 0.506779, 0.709798]]], Rectangle[{3 π/4, -2}, {5 π/4, 2}]}. ; ) That is the same blue tone as the plot line, and it has the vertical lines left and right. Thanks again! $\endgroup$
    – Britzel
    Jul 17, 2020 at 16:11
  • $\begingroup$ Sure, 'pimp' it to your heart's content. :) And, in case you didn't know, the default plot style is easier remembered as ColorData[97, 1]. $\endgroup$ Jul 17, 2020 at 16:52
  • $\begingroup$ Didn't know! Thanks once more. (I knew ColorData[97,"ColorList"] and then copy-pasted the desired color so far.) $\endgroup$
    – Britzel
    Jul 17, 2020 at 18:38

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.