3
$\begingroup$

I have problem with displaying variables in scientific notation in plot title. Below is my code sample,

ClearAll;
f1[a_,b_,c_]:=a*x^2+b*x^3+c;
f2[a_,b_,c_]:=a*x+b*x^2+(2-x)*c;
x=Range[0,10,0.01];
a1=1.0;b1=2.0;c1=0.000000001;
f3=f2[a1,b1,c1]+f1[a1,1,c1];
plot1=ListPlot[f3,PlotLabel->Style[" a="<>ToString[a1]<>"   b="<>ToString[b1]<>" c="<>ToString[c1]]]

It works on simple decimal numbers. However, longer decimals do not display properly. How to fix that?

$\endgroup$
1
  • 2
    $\begingroup$ With that many data points ListLinePlot produces a clearer, sharper plot. However, ListPlot has an advantage if you you want to include Tooltip. $\endgroup$
    – Bob Hanlon
    Commented Dec 6, 2017 at 16:17

3 Answers 3

2
$\begingroup$

I don't see any reason to use ToString, and I especially avoid using ToString[expr, TraditionalForm] if at all possible. Instead, I would do:

ListPlot[
    f3,
    PlotLabel->Row[{a==a1, b==b1, c==c1},"    "]
]

enter image description here

$\endgroup$
2
  • $\begingroup$ Until now I also used often ToString to produce labels in plots, What is the disadvantage of it? $\endgroup$
    – mrz
    Commented Dec 7, 2017 at 20:09
  • 1
    $\begingroup$ @mrz Anything you can do with ToString you should be able to do with Row. ToString has issues. For instance ToString[1.1*^-8] or ToString[Subscript[x,1]]. And I don't like StandardForm/TraditionalForm because I don't like linear syntax (e.g., "\!(*SubscriptBox[(x), (1)])"). Finally, InputForm typically isn't useful (e.g., ToString[Subscript[x, 1], InputForm]). I much prefer creating an expression instead of a string as a label. $\endgroup$
    – Carl Woll
    Commented Dec 7, 2017 at 20:34
1
$\begingroup$
PlotLabel -> 
 Style[" a=" <> ToString[a1, TraditionalForm] <> "   b=" <> 
   ToString[b1, TraditionalForm] <> " c=" <> 
   ToString[c1, TraditionalForm]]
$\endgroup$
1
$\begingroup$

I am not sure if you want to write all decimals or not. I am assuming the first case as the statement says "It works on simple decimal numbers". Thus, be advised you can increase the threshold for representing number in the scientific form by using ScientifcNotationThreshold in NumberForm along the numerator of digits and decimals, and along the suggestion of @BobHanlon for a clearer plot:

ClearAll;
f1[a_, b_, c_] := a*x^2 + b*x^3 + c;
f2[a_, b_, c_] := a*x + b*x^2 + (2 - x)*c;
x = Range[0, 10, 0.01];
a1 = 1.0000000000034; b1 = 2.0; c1 = 0.000000001;
f3 = f2[a1, b1, c1] + f1[a1, 1, c1];
plot1 = ListLinePlot[f3, 
        PlotLabel -> 
       Row[Style[#, 12, Black, Bold, SingleLetterItalics -> False] & /@
       {a ==NumberForm[a1, {18, 17}, ScientificNotationThreshold -> {-25, 25}],
       b == b1, 
       c == NumberForm[c1, {10, 9}, ScientificNotationThreshold -> {-15, 15}]
       }, Invisible["spc"]
       ]
]

enter image description here

enter image description here

Edit

The suggestions by Carl have been added (avoid use of ToString whenever posible). It is also included a second plot when the label is dangerously long by using Column with option Aligment-> Left, instead of Row.

$\endgroup$
8
  • $\begingroup$ If you change b to 2*^-17 the output is horrendous. This is one of the reasons I strongly discourage the use of single-argument ToString. $\endgroup$
    – Carl Woll
    Commented Dec 7, 2017 at 18:16
  • $\begingroup$ I am not sure what you mean. However, guessing it, just use NumberForm as for a1 and c1... $\endgroup$ Commented Dec 7, 2017 at 18:18
  • $\begingroup$ I'm sorry, I meant change a1 = 1.0000000000034 to a1=2.0*^-17. $\endgroup$
    – Carl Woll
    Commented Dec 7, 2017 at 18:24
  • $\begingroup$ @CarlWoll change to ToString@NumberForm[a1, {18, 17}, ScientificNotationThreshold -> {-25, 25}] ;)) $\endgroup$ Commented Dec 7, 2017 at 18:27
  • $\begingroup$ Oh @CarlWoll, if row makes a very long label, then change Row to Column $\endgroup$ Commented Dec 7, 2017 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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