2
$\begingroup$

I have an automated process that produces plots that will be on a y-axis range of somewhere between zero and 1. I need to highlight certain bands in the plot that are a result of calculations in the process.

Using Epilog or Prolog seems a sensible approach but these options are not plotting when the y-axis range is "sufficiently" small.

For example, say the process calculates the following bands

bands = Partition[10 ^6 {100, 250, 500, 1300, 2500}, 2, 1]

for a derived function

Plot[x 10^-17, {x, 50 10^6, 500 10^6}]

Mathematica graphics

I need to highlight the bands with, for example,

Graphics@MapIndexed[
   {Opacity[.25, ColorData[110][First@#2]], 
    Rectangle[{Last@#1, 0}, {Total@#1, 1}]
   } &, bands]

Mathematica graphics

But this produces an empty Graphics.

After some trial and error I discovered that the bands will show if I explicitly set the AspectRatio.

Graphics[
    MapIndexed[
     {Opacity[.25, ColorData[110][First@#2]], 
      Rectangle[{Last@#1, 0}, {First@#1, 1}]
     } &, bands], 
    AspectRatio -> 1/GoldenRatio]

Mathematica graphics

However, applying the same option to the plot does not produce the highlighted bands.

Plot[x 10^-17, {x, 50 10^6, 500 10^6},
 AspectRatio -> 1/GoldenRatio, 
 Epilog -> 
  MapIndexed[
    {Opacity[.25, ColorData[110][First@#2]], 
     Rectangle[{Last@#1, 0}, {First@#1, 1}]
    } &, bands]
]

Mathematica graphics

and even applying AspectRatio liberally in Show does not result in the desired plot.

Show[
 Plot[x 10^-17, {x, 50 10^6, 500 10^6}, AspectRatio -> 1/GoldenRatio]
 ,
 Graphics[
  MapIndexed[
    {Opacity[.25, ColorData[110][First@#2]], 
     Rectangle[{Last@#1, 0}, {First@#1, 1}]
    } &, bands],
  AspectRatio -> 1/GoldenRatio],
 AspectRatio -> 1/GoldenRatio
 ]

Mathematica graphics

Any ideas how to get around this. Please note that I do not know what the function or the bands will be until near the end of the process. I am looking for a general solution of which the above is a basic example.

Also, can anyone else replicate this issue as I believe it to be a bug. Mma 11.3 on Win 10.

$\endgroup$
  • $\begingroup$ Try restarting your kernel -- I see the plot with the stripes. $\endgroup$ – bill s Nov 26 '18 at 22:08
  • $\begingroup$ @bills Yeah, I tried that before posting. Even restarted Mma. No joy. What version and OS are you using? $\endgroup$ – Edmund Nov 26 '18 at 22:46
1
$\begingroup$
plt0 = Plot[x 10^-17, {x, 50 10^6, 500 10^6}];
yrange = PlotRange[plt0][[2]];
epilog = MapIndexed[{Opacity[.25, ColorData[110][First@#2]], 
   Rectangle[{Last@#1, First@yrange}, {First@#1, Last@yrange}]} &, bands];

Show[plt0, Epilog -> epilog]

enter image description here

$Version

"11.3.0 for Microsoft Windows (64-bit) (March 7, 2018)"

| improve this answer | |
$\endgroup$
  • $\begingroup$ Que! Since when has PlotRange been a function that would return the PlotRange AbsoluteOptions of a graphic? This is news to me; and apparently to the documentation as well. $\endgroup$ – Edmund Nov 26 '18 at 23:46
  • $\begingroup$ @Edmund, it is undocumented. It has been used in a number of posts on this site: see this and the links there $\endgroup$ – kglr Nov 26 '18 at 23:58
0
$\begingroup$

Your code works with v11.3 on my Mac

$Version

(* "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)" *)

Note that AspectRatio -> 1/GoldenRatio is the default for Plot

Options[Plot, AspectRatio]

(* {AspectRatio -> 1/GoldenRatio} *)

Plot[x 10^-17, {x, 50 10^6, 500 10^6}, 
 Epilog -> MapIndexed[{Opacity[.25, ColorData[110][First@#2]], 
     Rectangle[{Last@#1, 0}, {First@#1, 1}]} &, bands]]

enter image description here

I recommend that you use Prolog and PlotStyle to make the Plot stand out better from the bands.

Plot[x 10^-17, {x, 50 10^6, 500 10^6},
 PlotStyle -> Thick, 
 Prolog -> MapIndexed[{Opacity[.25, ColorData[110][First@#2]], 
     Rectangle[{Last@#1, 0}, {First@#1, 1}]} &, bands]]

enter image description here

| improve this answer | |
$\endgroup$

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.