3
$\begingroup$

I am trying to combine two plots using the Show function and have the resulting legend appear as a row. Using the LegendLayout function for each individual plot is not working. Below is my code:

Plot300 = 
  Plot[Exp[-0.03*t], {t, 0, 100}, 
   PlotLegends -> LineLegend[{Style["Hello"]}, LegendLayout -> "Row"],
    PlotStyle -> Blue];
Plot400 = 
  Plot[Exp[-0.03*t], {t, 0, 100}, 
   PlotLegends -> 
    LineLegend[{Style["Hello2"]}, LegendLayout -> "Row"], 
   PlotStyle -> Green];
P1=Show[Plot300, Plot400]

Is there a straightforward solution to this problem? Any suggestions would be greatly appreciated!

$\endgroup$
2
  • $\begingroup$ Are workarounds acceptable? $\endgroup$
    – bmf
    Apr 19, 2022 at 3:42
  • $\begingroup$ @bmf Potentially, yes! Any suggestions would be helpful $\endgroup$
    – MathIsHard
    Apr 19, 2022 at 3:48

2 Answers 2

5
$\begingroup$

One possible workaround is to use the Labeled function and build up a legend according to your requirements.

Plot300 = Plot[Exp[-0.03*t], {t, 0, 100}, PlotStyle -> Blue];
Plot400 = Plot[Exp[-0.09*t], {t, 0, 100}, PlotStyle -> Green];
P1 = Labeled[
  Show[Plot300, Plot400]
  , LineLegend[
   {Blue, Green}
   , {Style["Hello2"], Style["Hello"]}
   , LegendLayout -> "Row"
   ]
  , Bottom
  ]

enter image description here

$\endgroup$
3
  • $\begingroup$ Why is the Labeled function needed here? $\endgroup$
    – MathIsHard
    Apr 19, 2022 at 17:14
  • $\begingroup$ I am not sure I understand your question. One of my thoughts, as I was composing this answer, was that it allows the addition of a legend independent of the plots. $\endgroup$
    – Syed
    Apr 19, 2022 at 17:42
  • $\begingroup$ Oh I understand! Thank you so much $\endgroup$
    – MathIsHard
    Apr 19, 2022 at 17:49
3
$\begingroup$
aux = {"Hi bmf", "Hi mathbioguy. I hope you'll find this useful"};
aux1 = Map[Style[#, 13] &, Table[aux[[i]], {i, 1, Length@aux}]];
pos = Bottom;
f1[x_] := Exp[-0.03*x]
f2[x_] := x^2
plot1 = Plot[f1[x], {x, -2, 2}, PlotRange -> {{-2, 2}, {0, 4}}, 
   PlotLegends -> Placed[LineLegend[{aux[[1]]}], pos], 
   PlotStyle -> {Blue, Dashed, Thick}];
plot2 = Plot[f2[x], {x, -2, 2}, PlotRange -> {{-2, 2}, {0, 4}}, 
   PlotLegends -> Placed[LineLegend[{aux[[2]]}], pos], 
   PlotStyle -> {Red, Thick}];
Show[plot1, plot2]

plot

$\endgroup$

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.