1
$\begingroup$

How can I make a framed legend in the question answered by Alex here: Numerically solving a system of SDE's with Levy noise?

When I try the "normal" way, it doesn't do anything.

The code:

 SeedRandom[1234];
    A (*Recruitment rate*)= { 0.9, 0.3, 0.6, 0.6};
    µ1 (*Natural mortality rate of S*)= { 0.3, 0.3, 0.4, 0.4};
    \[Beta] (*Transmission rate*) = {0.07, 1.3, 0.35, 0.8};
    \[Gamma] (*Recovered rate*)= { 0.05, 0.05, 0.2, 0.3};
    µ2 (*General mortality of I*)= { 0.5, 0.5, 0.3, 0.3};
    \[Eta] (*Exponentially fading memory rate*)= {0.09, 0.09, 0.7, 0.2};
    \[Sigma]1 (*Intensity of W1(t)*)= { 0.15, 0.15, 0.2, 0.169};
    \[Sigma]2 (*Intensity of W2(t)*)= { 0.25, 0.25, 0.15, 0.15};
    \[Sigma]4 (*Intensity of W4(t)*)= { 0.27, 0.27, 0.13, 0.13};
    \[Lambda]1 (*Jump intensity of S*)= { 0.2, 0.2, 0.5, 0.5};
    \[Lambda]2 (*Jump intensity of I*)= { 0.23, 0.23, 0.3, 0.3};
    \[Lambda]4 (*Jump intensity of D*)= { 0.1, 0.1, 0.7, 0.7};
    
    tmax = 301; pWe1 = 
     RandomFunction[WhiteNoiseProcess[], {0, tmax}][[2, 1, 1]]; dW1 = 
     Interpolation[Table[{(j - 1), pWe1[[j]]}, {j, Length[pWe1]}], 
      InterpolationOrder -> 1]; pWe2 = 
     RandomFunction[WhiteNoiseProcess[], {0, tmax}][[2, 1, 1]]; dW2 = 
     Interpolation[Table[{(j - 1), pWe2[[j]]}, {j, Length[pWe2]}], 
      InterpolationOrder -> 1]; pWe4 = 
     RandomFunction[WhiteNoiseProcess[], {0, tmax}][[2, 1, 1]]; dW4 = 
     Interpolation[Table[{(j - 1), pWe4[[j]]}, {j, Length[pWe4]}], 
      InterpolationOrder -> 1];
    
    pL1 = RandomFunction[PoissonProcess[1.], {0, tmax}]; pL2 = 
     RandomFunction[PoissonProcess[1.1], {0, tmax}]; pL4 = 
     RandomFunction[PoissonProcess[.9], {0, tmax}]; ListStepPlot[{pL1, 
      pL2, pL4}] 
    dpL1 = pL1["SliceData", Range[tmax]] // First // Differences; dpL2 = 
     pL2["SliceData", Range[tmax]] // First // Differences; dpL4 = 
     pL4["SliceData", Range[tmax]] // First // Differences;
    
    L1[t_] := If[t < 1, 0, dpL1[[Round[t]]]]/tmax; 
    L2[t_] := If[t < 1, 0, dpL2[[Round[t]]]]/tmax; 
    L4[t_] := If[t < 1, 0, dpL4[[Round[t]]]]/tmax; 
    eq1 = -s'[t] + (a - mu1 s[t] - beta s[t] d[t]) + sigma1 s[t] dW1[t] + 
       lambda1 s[t] L1[t];
    eq2 = -i'[t] + (beta s[t] d[t] - (mu2 + gamma) i[t]) + 
       sigma2 i[t] dW2[t] + lambda2 i[t] L2[t];
    eq3 = -r'[t] + (gamma i[t] - mu3 r[t]);
    eq4 = -d'[t] + eta (i[t] - d[t]) + sigma4 d[t] dW4[t] + 
       lambda4 d[t] L4[t];
    
    ic = {s[0] == 0.6, i[0] == 0.3, d[0] == 0.05}; 
    rul[j_] := {a -> A[[j]], beta -> \[Beta][[j]], 
      gamma -> \[Gamma][[j]], eta -> \[Eta][[j]], mu1 -> µ1[[j]], 
      mu2 -> µ2[[j]], sigma1 -> \[Sigma]1[[j]], sigma2 -> \[Sigma]2[[j]], 
      sigma4 -> \[Sigma]4[[j]], lambda1 -> \[Lambda]1[[j]], 
      lambda2 -> \[Lambda]2[[j]], lambda4 -> \[Lambda]4[[j]]};
    eqn[j_]: = {eq1, eq2, eq4} /. rul[j];
    
    sol[j_]: = NDSolve[{eqn[j] == {0, 0, 0}, ic}, {s, i, d}, {t, 0, tmax - 1}];
    With[{sol = sol[1]}, {Plot[Evaluate[s[t] /. sol], {t, 0, tmax - 1}, 
       PlotRange -> All, Frame -> True, PlotStyle -> Green, 
       FrameLabel -> {"Time t (Days)", "S"},PlotLegends -> 
 Placed[LineLegend[Green, "S(t)", LegendFunction -> Framed]]], 
      Plot[Evaluate[i[t] /. sol], {t, 0, tmax - 1}, PlotRange -> All, 
       Frame -> True, PlotStyle -> Blue, 
       FrameLabel -> {"Time t (Days)", "I"},PlotLegends -> 
 Placed[LineLegend[Blue, "I(t)", LegendFunction -> Framed]]], 
      Plot[Evaluate[d[t] /. sol], {t, 0, tmax - 1}, PlotRange -> All, 
       Frame -> True, PlotStyle -> Red, 
       FrameLabel -> {"Time t (Days)", "D"},PlotLegends -> 
 Placed[LineLegend[Red, "I(t)", LegendFunction -> Framed]]]}]
$\endgroup$
4
  • $\begingroup$ Your code produces four plots. You have neither said nor shown where you are trying to add the legend, nor what he legend should be. This is not a minimal working example illustrating the problem that you are having. $\endgroup$
    – Bob Hanlon
    Jan 23, 2023 at 17:23
  • $\begingroup$ The 3 plots that are produced from the code, I would like to add in framed legends inside the plot. Something like PlotLegends -> Placed[LineLegend[{Blue, Orange, Red}, {"S(t)", "A(t)", "I(t)"}, LegendFunction -> Framed] This was taken for a plot with all 3 lines of the same plot. $\endgroup$
    – Math
    Jan 23, 2023 at 17:25
  • $\begingroup$ Edit your question. Eliminate the plot or plots that are not involved with the required legend. Show what you have tried so that it is clear what you are trying to do. $\endgroup$
    – Bob Hanlon
    Jan 23, 2023 at 17:34
  • $\begingroup$ @BobHanlon Edited. I am trying to put the legends -(Green line) S(t), - (blue line) I(t) and -(red line) D(t) in each respective plot. $\endgroup$
    – Math
    Jan 23, 2023 at 18:46

1 Answer 1

3
$\begingroup$

As shown in the documentation, you must use lists for the colors and labels in LineLegend. And a position must be provided to Placed

With[{sol = sol[1]}, {
  Plot[Evaluate[s[t] /. sol], {t, 0, tmax - 1},
   PlotRange -> All,
   Frame -> True,
   PlotStyle -> Green,
   FrameLabel -> {"Time t (Days)", "S"},
   ImageSize -> Medium,
   PlotLegends -> Placed[
     LineLegend[{Green}, {"S(t)"},
      LegendFunction -> Framed],
     {.8, .2}]], Plot[Evaluate[i[t] /. sol], {t, 0, tmax - 1},
   PlotRange -> All,
   Frame -> True,
   PlotStyle -> Blue,
   FrameLabel -> {"Time t (Days)", "I"},
   ImageSize -> Medium,
   PlotLegends -> Placed[
     LineLegend[{Blue}, {"I(t)"},
      LegendFunction -> Framed],
     {.8, .8}]],
  Plot[Evaluate[d[t] /. sol], {t, 0, tmax - 1},
   PlotRange -> All,
   Frame -> True,
   PlotStyle -> Red,
   FrameLabel -> {"Time t (Days)", "D"},
   ImageSize -> Medium, PlotLegends -> Placed[
     LineLegend[{Red}, {"I(t)"},
      LegendFunction -> Framed],
     {.8, .8}]]}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Note that the list of colors is unnecessary in these cases as they can be inherited. $\endgroup$
    – Bob Hanlon
    Jan 23, 2023 at 19:35
  • $\begingroup$ If you use SetOptions[LineLegend, LegendFunction -> Framed]; then the lines with PlotLegends can be simplified to the form PlotLegends -> Placed[{"S(t)"}, {.8, .2}] $\endgroup$
    – Bob Hanlon
    Jan 24, 2023 at 5:04

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.