Consider:
tmax = 100;
b = 0.1;
n[t_] := S[t] + P[t]
SP = NDSolveValue[{
S'[t] == (-b/n[t])*S[t]*P[t],
P'[t] == (b/n[t])*S[t]*P[t],
S[0] == 0.99,
P[0] == 0.01},
{S, P},
{t, 0, tmax}];
Plot[{Through[SP[t]], Total@Through[SP[t]]} // Evaluate, {t, 0, tmax},
PlotStyle -> {Blue, Red, Dashed}, Frame -> True,
FrameLabel -> {Style[Time, Black, FontSize -> 15],
Style[Density, Black, FontSize -> 15]},
PlotLegends -> Placed[{"A(t)", "B(t)"}, {Right, Top}]]
How do I plot the legends such that it doesn't overlap? for different inputs the graph changes so is there an automatic way for it not to overlap?
Side questions:
i) How do I put a box around the legends after they've been fixed?
ii) How to insert extra parameter in legends without it being a graph in the plot?
iii) How to put another dashed line at 0(and make the dashed lines both black in colour)?
EDIT(using Szabolcs's solution):
tmax = 100;
b = 0.2;
g = 0.2;
n[t_] := S[t] + P[t] + R[t]
SPR = NDSolveValue[{
S'[t] == (-b/n[t])*S[t]*P[t],
P'[t] == (b/n[t])*S[t]*P[t] - g*P[t],
R'[t] == g*P[t],
S[0] == 0.95,
P[0] == 0.05,
R[0] == 0},
{S, P, R},
{t, 0, tmax}];
{f1, f2, f3} = SPR;
st = Style[#, 15, Black] &;
Plot[{f1[t], f2[t], f3[t], f1[t] + f2[t] + f3[t], 0}, {t, 0, tmax},
PlotStyle -> {Blue, Red, Green, Directive[Black, Dashed],
Directive[Black, Dashed]}, Frame -> True,
FrameLabel -> st /@ {"Time", "Density"},
PlotLegends ->
Placed[LineLegend[{Blue, Red, Green, Black}, {"A(t)", "B(t)",
"C(t)"}, LegendFunction -> Framed], {0.85, 0.65}]]
We have overlapping; how can we place the legends automatically such that it doesn't overlap the graph?
EDIT 2
I manually shifted the legends and it worked fine.
PlotLegends
in the docs, specifically Scope -> Legend Placement -> last example. $\endgroup$