2
$\begingroup$

I have an issue about putting a legend consisted of a text and numerical value instead of a label.

For example, with this command line :

ωBD=9900;
pl = ListLinePlot[{ScaleFactorPast,ScaleFactorFuture},PlotStyle->{Blue,Blue},Frame->True,FrameLabel->{"t","a(t)"},LabelStyle->{FontSize->20,FontFamily->"Helvetica",Black,Bold},PlotRange->All, ImageSize->{600},Epilog->{Style[Text["Now",{0,1.4}],24],PointSize[.02],Point[{0, 1}]}, PlotLabel -> Row[{Subscript[ω, BD], "=", ωBD}]]

which generates the following plot :

right label

Now, I would like to have the same label (text=omega_BD = numerical value associated) but in a legend box with same rendring than for the simple label printing.

I would like to put it either with a smart way (like in Python Matplotlib loc='best' or at the bottom right (where there is some void).

I don't know how to perform this, i.e put the initial label into a legend with ListLinePlot function.

Any tracks or suggestions are welcome.

UPDATE 1 : This solution doesn't work :

 pl = ListLinePlot[{ScaleFactorPast,ScaleFactorFuture},PlotStyle->{Blue,Blue},Frame->True,FrameLabel->{"t","a(t)"},LabelStyle->{FontSize->20,FontFamily->"Helvetica",Black,Bold},PlotRange->All, ImageSize->{600},Epilog->{Style[Text["Now",{0,1.4}],24],PointSize[.02],Point[{0, 1}]}];
legend =
  LineLegend[{Black}, {Row[{Subscript[\[Omega], BD],
      "=", \[Omega]BD}]},
   LegendFunction -> (Framed[#, FrameMargins -> 0,
       Background -> White] &),
   LegendLabel ->
    Style["The legend is shown on top", FontFamily -> "Helvetica", 17,
      Blue],
   LabelStyle -> {FontFamily -> "Helvetica", 24, Black, Italic, Bold}];
Legended[Show[pl], Placed[legend, {0, 1}]]
pics = Join[pics, {
Text[Style["Scale factor behavior for BD"], Background->Hue[0.45, 0.5, 1]],
pl
}];
Export["output/BD_plot.png", pl];
Pause[5]

see the result below :

no legend box

No box legend appears, I have missed something but what ?

UPDATE 2: Here the results with you copy/paste code.

missing legend box

As you can see, only a black big point and the text "Now" appear, no box legend.

UPDATE 3 : the script generates a legend as expected but only Mathematica. Actually, from the beginning, I launch directly the script from command line terminal by doing :

$ wolframscript script_plot.wls

I would be curious to know why it works in Mathematica and not with wolframscript interpreter.

Maybe there is a workaround to get the same figure with wolframscript interpreter ?

$\endgroup$
3
  • $\begingroup$ It might be a good idea to either unaccept my answer so people can have a look at what's going on, or ask a new question along the lines of differences between mathematica and wolframscript. I am just suggesting this so you can receive more attention to your issue. Also, the second option might be better for site maintenance bit the more experienced users should know better. $\endgroup$
    – bmf
    Apr 14 at 22:00
  • $\begingroup$ I have unaccept your answer, don't blame me, we have found at least on Mathematica the same plot. Regards $\endgroup$
    – youpilat13
    Apr 14 at 22:17
  • $\begingroup$ No blame at all. Don't worry about it :) Just a suggestion: perhaps you should start by saying that you are running wolframscript on a mac and update the title of the question as well, so the rest of users are aware. It's a significant piece of information and in my opinion it should not be at the end of the question. That's the main issue. As you said, in a Mathematica notebook my suggested solution works ;) $\endgroup$
    – bmf
    Apr 14 at 23:04

2 Answers 2

2
$\begingroup$

Generate some random data

ScaleFactorPast = RandomReal[10, 100];
ScaleFactorFuture = RandomReal[5, 100];

Edit: addressing the comment by the author of the OP

then we do

p1 = ListLinePlot[{ScaleFactorPast, ScaleFactorFuture}, 
   PlotStyle -> {Blue, Blue}, Frame -> True, 
   FrameLabel -> {"t", "a(t)"}, 
   LabelStyle -> {FontSize -> 20, FontFamily -> "Helvetica", Black, 
     Bold}, PlotRange -> All, ImageSize -> {600}];
legend = 
  LineLegend[{Black}, {Row[{Subscript[\[Omega], BD], 
      "=", \[Omega]BD}]}, 
   LegendFunction -> (Framed[#, FrameMargins -> 0, 
       Background -> White] &), 
   LegendLabel -> 
    Style["The legend is shown on top", FontFamily -> "Helvetica", 17,
      Blue], 
   LabelStyle -> {FontFamily -> "Helvetica", 24, Black, Italic, Bold}];
Legended[Show[p1], Placed[legend, {.5, 1.05}]]

update

To change the position just manipulated the numbers in the Placed command. Example

p1 = ListLinePlot[{ScaleFactorPast, ScaleFactorFuture}, 
   PlotStyle -> {Blue, Blue}, Frame -> True, 
   FrameLabel -> {"t", "a(t)"}, 
   LabelStyle -> {FontSize -> 20, FontFamily -> "Helvetica", Black, 
     Bold}, PlotRange -> All, ImageSize -> {600}];
legend = 
  LineLegend[{Black}, {Row[{Subscript[\[Omega], BD], 
      "=", \[Omega]BD}]}, 
   LegendFunction -> (Framed[#, FrameMargins -> 0, 
       Background -> White] &), 
   LegendLabel -> 
    Style["The legend is shown on top", FontFamily -> "Helvetica", 17,
      Blue], 
   LabelStyle -> {FontFamily -> "Helvetica", 24, Black, Italic, Bold}];
Legended[Show[p1], Placed[legend, {.3, .2}]]

update3


Original answer


p1 = ListLinePlot[{ScaleFactorPast, ScaleFactorFuture}, 
   PlotStyle -> {Blue, Blue}, Frame -> True, 
   FrameLabel -> {"t", "a(t)"}, 
   LabelStyle -> {FontSize -> 20, FontFamily -> "Helvetica", Black, 
     Bold}, PlotRange -> All, ImageSize -> {600}];
legend = 
  LineLegend[{Black}, {Row[{Subscript[\[Omega], BD], 
      "=", \[Omega]BD}]}, 
   LegendLabel -> 
    Style["The legend is shown on top", FontFamily -> "Helvetica", 17,
      Blue], 
   LabelStyle -> {FontFamily -> "Helvetica", 24, Black, Italic, Bold}];
Legended[Show[p1], Placed[legend, {.5, 1.05}]]

plot2


Final edit: a screenshot

The code for the screenshot so the author of the OP can copy and paste easily

ScaleFactorPast = RandomReal[10, 100];
ScaleFactorFuture = RandomReal[5, 100];
\[Omega]BD = 9900;
pl = ListLinePlot[{ScaleFactorPast, ScaleFactorFuture}, 
   PlotStyle -> {Blue, Blue}, Frame -> True, 
   FrameLabel -> {"t", "a(t)"}, 
   LabelStyle -> {FontSize -> 20, FontFamily -> "Helvetica", Black, 
     Bold}, PlotRange -> All, ImageSize -> {600}, 
   Epilog -> {Style[Text["Now", {3.1, 1.7}], 24], PointSize[.02], 
     Point[{0, 1}]}];
legend = 
  LineLegend[{Black}, {Row[{Subscript[\[Omega], BD], 
      "=", \[Omega]BD}]}, 
   LegendFunction -> (Framed[#, FrameMargins -> 0, 
       Background -> White] &), 
   LegendLabel -> 
    Style["The legend is shown on top", FontFamily -> "Helvetica", 17,
      Blue], 
   LabelStyle -> {FontFamily -> "Helvetica", 24, Black, Italic, Bold}];
Legended[Show[pl], Placed[legend, {.35, .25}]]

screenshot


$\endgroup$
17
  • $\begingroup$ Thanks for your quick answer. Actually, I have mainly need to put a box legend inside the plot since there is some permanent void space (always on the bottom right of my figure) : how can I specify this location ? I know in python that matplotlib allows to put legend at different locations without indicating the (x,y) coordinates inside. Thanks in advance. $\endgroup$
    – youpilat13
    Apr 14 at 18:52
  • $\begingroup$ if possible, I would like to get a box legend $\endgroup$
    – youpilat13
    Apr 14 at 18:52
  • $\begingroup$ @stefan I am showing you how to do both of those things in the updated version. Can you please have a look and let me know if that's what you wanted? $\endgroup$
    – bmf
    Apr 14 at 19:01
  • $\begingroup$ fine ! this is exactly what I wanted, Best egards $\endgroup$
    – youpilat13
    Apr 14 at 20:00
  • 1
    $\begingroup$ I am going to test it right now, thanks $\endgroup$
    – youpilat13
    Apr 14 at 21:16
2
$\begingroup$

You can use the PlotLegends option with relative position syntax of Placed.

With

ωBD = 9900;
baseStyle = {FontSize -> 20, FontFamily -> "Helvetica", Black, Bold};

Then

Plot[x^2
 , {x, 0, 3}
 , PlotLegends ->
  Placed[
   {Style[Row[{Subscript[ω, BD], "=", ωBD}], baseStyle]}
   , {{.05, .95}, {0, 1}}
   ]
 , BaseStyle -> baseStyle
 , Frame -> True
 ]

enter image description here

Placed can take Top, Bottom, and others.

Hope this helps.

$\endgroup$
5
  • $\begingroup$ thanks. Could you indicate me the meaning of {.05, .95}, {0, 1}} ? : I don't know the utility of {0,1} . Regards $\endgroup$
    – youpilat13
    Apr 14 at 20:31
  • $\begingroup$ @stefan {scaled position in the plot, scaled position in the legend}. So {0,1} is top left-hand corner. $\endgroup$
    – Edmund
    Apr 14 at 20:38
  • $\begingroup$ fine ! and what about the firs couple {.05, .95} ? $\endgroup$
    – youpilat13
    Apr 14 at 20:50
  • $\begingroup$ That is the scaled position in the plot, x-y scaled coordinates. $\endgroup$
    – Edmund
    Apr 14 at 20:52
  • $\begingroup$ I make confusions between these 2 couples ? I only need a couple, not ? $\endgroup$
    – youpilat13
    Apr 14 at 20:55

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.