7
$\begingroup$

Imagine a set of curves

data = Transpose@Table[{Exp[t], Exp[1.1 t], Exp[1.2 t], Exp[1.3 t]},
    {t, 0, 5, 0.1}];

which should be labelled with numbers

numbers = {0.1234, 1.234, 123.4, 1234.};
legendForm = ScientificForm[#, 3] & /@ numbers

nicely formatted by ScientificForm or EngineeringForm or etc.. For one PlotLegends the output is as expected

ListLinePlot[data,
 Frame -> True,
 PlotLegends -> Placed[#, {{Left, Top}, {Left, Top}}] &@
  LineLegend[Automatic, legendForm,
   LegendLabel -> "Legend 1", Spacings -> 0.2],
 PlotRange -> All]

One legend

but for two PlotLegends the number format is lost:

ListLinePlot[data,
 Frame -> True,
 PlotLegends -> {
   Placed[#, {{Left, Top}, {Left, Top}}] &@
    LineLegend[Automatic, legendForm,
     LegendLabel -> "Legend 1", Spacings -> 0.2],
   Placed[#, {{Left, Top}, {Left, Top}}] &@
    LineLegend[Automatic, legendForm,
     LegendLabel -> "Legend 2", Spacings -> 0.2]},
 PlotRange -> All]

Two legends

Is this behaviour a bug or are the NumberForm functions not suitable to format PlotLegends? What would be an alternative method?

$\endgroup$
  • $\begingroup$ I thing the alternative would be to make string like this "1.23[Times]\!(*SuperscriptBox[(10), (-1)])" automatically from ScientificForm, which I can get from just putting quotes around numbers manually. Obvious ways like ToString or TextString doesn't work as expected, so probably it requires just another implementaion of ScientificForm that would produce a nice string. $\endgroup$ – swish Mar 15 '16 at 0:11
  • $\begingroup$ @swish: Can you tell more details on how to generate the string by manually inserting quotes? For me this procedure is not working, maybe due to the Mathematica version - I use 10.3? What do you mean with "automatically from ScientificForm" if a manual quote insertion is required? Do you suggest with "another implementation of ScientificForm" to define a custom ScientificForm function or the necessity for Wolfram Research to update the built-in function? $\endgroup$ – Choronaut Mar 15 '16 at 20:29
  • $\begingroup$ I just tried typing "123[ESC]*[ESC]10[CTRL-6]^-1[CTRL-SPACE]" and the result was a string that displayed fine inside legends. $\endgroup$ – swish Mar 15 '16 at 20:39
4
$\begingroup$

If you run FullForm on two cases, you will see that the first case contains:

List[ScientificForm[0.1234, 3], ScientificForm[1.234, 3], 
 ScientificForm[123.4, 3], ScientificForm[1234., 3]]

and the second one contains:

List[0.1234`, 1.234`, 123.4`, 1234.`]

I'm not sure why. But workaround would be:

ListLinePlot[data, Frame -> True, 
  PlotLegends -> {Placed[#, {{Left, Top}, {Left, Top}}] &@
     LineLegend[Automatic, numbers, LegendLabel -> "Legend 1", 
      Spacings -> 0.2], 
    Placed[#, {{Left, Top}, {Left, Top}}] &@
     LineLegend[Automatic, numbers, LegendLabel -> "Legend 2", 
      Spacings -> 0.2]}, PlotRange -> All] /. numbers -> legendForm

enter image description here

$\endgroup$
  • $\begingroup$ Nice workaround, which requires only a minimal amount of extra work and might only fail if the exact number sequence appears somewhere else in the plot output - an improbable scenario. $\endgroup$ – Choronaut Mar 15 '16 at 20:16
  • 1
    $\begingroup$ @Choronaut you can make the substitution rule more specific so it will actually affect LineLegend only. $\endgroup$ – BlacKow Mar 15 '16 at 20:23

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.