3
$\begingroup$

I used two dashed lines in the following codes to separate my plot into three blocks filled with color. But the fillings would obscure the curves (EtaH[n] and EpsilonH[n]).

fill1 = Rectangle[{zerosofeta[[1]], Log@(10^(-12))}, {zerosofeta[[2]],
     Log@4}];
fill2 = Rectangle[{0, Log@(10^(-12))}, {zerosofeta[[1]], Log@4}];
fill3 = Rectangle[{zerosofeta[[2]], Log@(10^(-12))}, {70, Log@4}];

LogPlot[{\[Epsilon]H[n],Abs[\[Eta]H[n]]}, {n, 0, 70},PlotRange -> {10^(-11), 4}, 
 Epilog -> {{Directive[Black, 
      Dashed], 
     Line[{{zerosofeta[[1]], Log@(10^(-12))}, {zerosofeta[[1]], 
        Log@4}}]}, {Directive[Black, Dashed], 
     Line[{{zerosofeta[[2]], Log@(10^(-12))}, {zerosofeta[[2]], 
        Log@4}}]},{LightGreen, fill1}, {LightBlue, fill2}, {LightBlue, fill3}}]

where zerosofeta is a two-element list used to put the roots of EtaH[n].

enter image description here

I have tried another way

curve = LogPlot[{\[Epsilon]H[n],Abs[\[Eta]H[n]]}, {n, 0, 70}, 
   PlotRange -> {10^(-11), 4}];
rectangles = 
  Graphics[{{LightGreen, fill1}, {LightBlue, fill2}, {LightBlue, 
     fill3},{Directive[Black, 
      Dashed], 
     Line[{{zerosofeta[[1]], Log@(10^(-12))}, {zerosofeta[[1]], 
        Log@4}}]}, {Directive[Black, Dashed], 
     Line[{{zerosofeta[[2]], Log@(10^(-12))}, {zerosofeta[[2]], 
        Log@4}}]}}];
Show[rectangles, curve]

but the axes disappear.

Do you have any idea?

updated: The functions etaH[n] and epsilonH[n] are composed of InterploatingFunction which is the numerical solution of a PDE. But I might have a generic one to replace etaH[n] for error finding

eta[n_] := Module[{e0 = 0.02, ni0 = 0.0, no0 = 33.2, s0 = 1.,
e1 = -6.3, ni1 = 33.2, no1 = 35.7, s1 = 0.5,
e2 = 0.3, ni2 = 35.7, no2 = 55, s2 = 1., ef = 3, nif = 55, nof = 65, sf = 2},
(e0/2) * (Tanh[(n - ni0)/s0] - Tanh[(n - no0)/s0]) +
(e1/2) * (Tanh[(n - ni1)/s1] - Tanh[(n - no1)/s1]) +
(e2/2) * (Tanh[(n - ni2)/s2] - Tanh[(n - no2)/s2]) +
(ef/2) * (Tanh[(n - nif)/sf] - Tanh[(n - nof)/sf])]

enter image description here

$\endgroup$
6
  • $\begingroup$ These are also undefined {εH[n], Abs[ηH[n]]} $\endgroup$
    – bmf
    Commented Feb 21, 2023 at 8:48
  • $\begingroup$ yep, the definitions are not shown here for briefness $\endgroup$
    – holywhat
    Commented Feb 21, 2023 at 8:51
  • 1
    $\begingroup$ In principle it is expected that you present code that can run. if the definitions are that lengthy, you can cook up some toy model functions. $\endgroup$
    – bmf
    Commented Feb 21, 2023 at 8:52
  • $\begingroup$ Hi there! Ideally you want to post a code that replicates as closely as possible the output that you describe. This makes error-finding easier on the community. Could you replace your functions and variables with 'generic' ones so that it could run on any machine? $\endgroup$
    – alex
    Commented Feb 21, 2023 at 11:35
  • 1
    $\begingroup$ Sure! Thank you for your reminder. First time asking a question here 😂 $\endgroup$
    – holywhat
    Commented Feb 21, 2023 at 12:25

2 Answers 2

9
$\begingroup$

Epilog is something that will bring the argument at the front of the LogPlot layer. If you put your rectangles in the Epilog then they will mask your functions.

You can instead use Prolog to have them in the background. This avoids the need to use multiple plots in the solution given by @bmf (although you should keep that method in mind as it is a very good one as well)

zerosofeta = {30, 
   38}; (*i have chosen the points to match yours roughly*)

fill1 = Rectangle[{zerosofeta[[1]], Log@(10^(-12))}, {zerosofeta[[2]],
     Log@4}];
fill2 = Rectangle[{0, Log@(10^(-12))}, {zerosofeta[[1]], Log@4}];
fill3 = Rectangle[{zerosofeta[[2]], Log@(10^(-12))}, {70, Log@4}];

LogPlot[{Sin[n]^2, 0.01 Sqrt[Tanh[n]]}, {n, 0, 70}, 
 PlotStyle -> {Blue, Red}, PlotRange -> {1. 10^-12, 4}, 
 PlotLegends -> {"func_1", "func_2"},
 
 (*create the prolog background*)

 Prolog -> {{LightGreen, fill1}, {LightBlue, fill2}, {LightBlue, 
    fill3}},
 
 (*now add your epilog with the lines-you could instead use Gridlines*)

 Epilog -> {{Black, Dashed, 
    Line[{{zerosofeta[[1]], Log[1. 10^-12]}, {zerosofeta[[1]], 
       Log[4]}}]},
   {Black, Dashed, 
    Line[{{zerosofeta[[2]], Log[1. 10^-12]}, {zerosofeta[[2]], 
       Log[4]}}]},
   Inset["Phase I", {15, Log[10^-7]}],
   Inset["Phase II", {33.8, Log[10^-7]}],
   Inset["Phase III", {55, Log[10^-7]}]
   }]

enter image description here

Note that you could have also used Gridlines instead of Lines and an Epilog.

Lines are generally not the best to use with LogPlot as there is a small (but a bit annoying ;) ) scaling that you need to perform. You can find this SE link useful.

$\endgroup$
4
  • 2
    $\begingroup$ (+1) very nice use and explanation of Prolog vs Epilog and their scope $\endgroup$
    – bmf
    Commented Feb 21, 2023 at 10:34
  • 1
    $\begingroup$ thanks! :) Admittedly, stacking plots is my go-to method for more complex plots, but here this felt simpler. Epilog is just a great little thing, but sometimes its sibling is just as great. $\endgroup$
    – alex
    Commented Feb 21, 2023 at 10:47
  • $\begingroup$ Thank you too! Also works well $\endgroup$
    – holywhat
    Commented Feb 21, 2023 at 12:39
  • $\begingroup$ Happy to hear it does. take care. $\endgroup$
    – alex
    Commented Feb 21, 2023 at 13:05
5
$\begingroup$

If I understand the problem correctly you want something like this:

overlay

So, essentially you want to use Overlay.

The code:

zerosofeta = {-2, 10};
f[n_] := Log[1/Sqrt[n - 1]]^3
g[n_] := 1/Log[n + 2]^2
fill1 = Rectangle[{zerosofeta[[1]], Log@(10^(-12))}, {zerosofeta[[2]],
     Log@4}];
fill2 = Rectangle[{0, Log@(10^(-12))}, {zerosofeta[[1]], Log@4}];
fill3 = Rectangle[{zerosofeta[[2]], Log@(10^(-12))}, {70, Log@4}];
p1 = LogPlot[{f[n], Abs[g[n]]}, {n, 0, 70}, 
   PlotRange -> {10^(-11), 4}, 
   Epilog -> {{Directive[Black, Dashed], 
      Line[{{zerosofeta[[1]], Log@(10^(-12))}, {zerosofeta[[1]], 
         Log@4}}]}, {Directive[Black, Dashed], 
      Line[{{zerosofeta[[2]], Log@(10^(-12))}, {zerosofeta[[2]], 
         Log@4}}]}}];
p2 = LogPlot[{f[n], Abs[g[n]]}, {n, 0, 70}, 
   PlotRange -> {10^(-11), 4}, 
   Epilog -> {{Directive[Black, Dashed], 
      Line[{{zerosofeta[[1]], Log@(10^(-12))}, {zerosofeta[[1]], 
         Log@4}}]}, {Directive[Black, Dashed], 
      Line[{{zerosofeta[[2]], Log@(10^(-12))}, {zerosofeta[[2]], 
         Log@4}}]}, {LightGreen, fill1}, {LightBlue, 
      fill2}, {LightBlue, fill3}}];
Overlay[{p2, p1}]
$\endgroup$
2
  • $\begingroup$ Thanks! It works $\endgroup$
    – holywhat
    Commented Feb 21, 2023 at 12:38
  • $\begingroup$ @holywhat glad I was able to help :-) $\endgroup$
    – bmf
    Commented Feb 21, 2023 at 12:41

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.