1
$\begingroup$

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: I still need to understand how to enlarge the font of the legend, but this is another problem

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}]] 
$\endgroup$
  • 3
    $\begingroup$ 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. $\endgroup$ – Alexei Boulbitch Jul 15 '14 at 9:04
  • $\begingroup$ 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 :) $\endgroup$ – kalmanIsAGameChanger Jul 15 '14 at 14:40
3
$\begingroup$

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]

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – kalmanIsAGameChanger Jul 15 '14 at 12:28
  • 1
    $\begingroup$ @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. $\endgroup$ – rcollyer Jul 15 '14 at 13:01
  • $\begingroup$ Ok Now it is working. I post the code in the following post. $\endgroup$ – kalmanIsAGameChanger Jul 15 '14 at 14:34
  • $\begingroup$ I edited the firs question as suggested by the admins. :) $\endgroup$ – kalmanIsAGameChanger Jul 15 '14 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.