# Legend in a Show

I am trying to put some legends on a graph but I am not able to do that.

This is my code:

k = 1;   (* J/K *)
T = 1;  (* K *)
nlm =
NonlinearModelFit[data, V, {M, n}, λ];
nlm["BestFitParameters"]
Show[ListPlot[data],
Plot[Normal[nlm], {λ, 0, 8}, PlotStyle -> Red],
Frame -> True, GridLines -> Automatic,
GridLinesStyle -> Directive[Gray, Dashed], AspectRatio -> 1.6,
ImageSize -> Large,
FrameLabel -> {"λ", "σ [MPa]"},
LabelStyle -> {FontFamily -> "Helvetica", 14}]


I tried to look on the internet already but I don´t understand how to do it. For example there is this discussion: Dynamic legends involving show

In which it looks like the same problem of mine, but I am not able to fully understand it.

I am really new with Mathematica, so please consider it.

Thanks, Fab.

EDIT: After discussing here, this is the new code which works for me. Of course as someone told me, it is not running this piece of code because you need the data but it can be really useful to understand how to have such a picture:

k = 1;   (* J/K *)
T = 1;  (* K *)

nlm = NonlinearModelFit[data, V, {M, n}, λ];
nlm["BestFitParameters"]
lgnd = Framed[
Column[{LineLegend[{Red}, {"data"}],
PointLegend[{Blue}, {Row[{"Experiment Data"}]}]}],
Background -> LightGray];
Legended[Show[ListPlot[data],
Plot[Normal[nlm], {λ, 0, 8}, PlotStyle -> Red],
Frame -> True, GridLines -> Automatic,
GridLinesStyle -> Directive[Gray, Dashed], AspectRatio -> 1.,
ImageSize -> Large,
FrameLabel -> {"λ", "σ [MPa]"},
LabelStyle -> {FontFamily -> "Helvetica", 22}],
Placed[lgnd, {0.82, 0.2}]]

• Fab, What is your problem? It is not explained in the text. Second, to evaluate your code one needs data. So you should somehow provide it, or simplify your code such that it does not require any external data. – Alexei Boulbitch Jul 15 '14 at 9:04
• My problem was that I was not able to have a legend inside a show environment also after reading some examples. Thanks to @ubpdqn I solved my problem. Thanks for the interest :) – kalmanIsAGameChanger Jul 15 '14 at 14:40

For illustrative purpose (please also search site and documentation):

toy = Table[{j, j^2 + RandomReal[{-3, 3}]}, {j, -4, 4, 0.1}];
mod = LinearModelFit[toy, {1, x, x^2}, x];
lp = ListPlot[toy, PlotStyle -> Red];
modp = Plot[mod[x], {x, -4, 4}, PlotStyle -> Blue];
lgnd = Framed[
Column[{PointLegend[{Red}, {"data"}],
LineLegend[{Blue}, {Row[{"fit: ", Normal@mod}]}]}],
RoundingRadius -> 20, Background -> LightBlue];
Legended[Show[lp, modp], lgnd]


• Thanks, now it is working and I have my legend, but it is on the right side of the graph, as in your picture. How can I put my legend inside the graph? Thanks a lot. – kalmanIsAGameChanger Jul 15 '14 at 12:28
• @minidiable wrap the legend in Placed, e.g. Legended[Show[lp, modp], Placed[lgnd, position]] where position is where you want to put it. Legended accepts quite a few values. – rcollyer Jul 15 '14 at 13:01
• Ok Now it is working. I post the code in the following post. – kalmanIsAGameChanger Jul 15 '14 at 14:34
• I edited the firs question as suggested by the admins. :) – kalmanIsAGameChanger Jul 15 '14 at 14:41