2
$\begingroup$

This seems to be a simple, but useful question.

I am plotting a basic function:

multiplier = 10^11;
func = 10^-11 x^2;
Plot[func*multiplier, {x, 0, 5}, 
  AxesLabel -> {x, "Distance, \!\(\*SuperscriptBox[\(10\), \(-11\)]\) m"}]

That gives the result I wish to see.

Imagine now that I wish not to write the multiplier manually in AxesLabel when I change it. The inverse multiplier needs to be converted to a string with an exponent (so as to produce the same final appearance). I have tried the ToString function with some parameters. For example:

multiplier = 10^11;
func = 10^-11 x^2;
Plot[func*multiplier, {x, 0, 5}, 
  AxesLabel -> 
    {x, "Distance, " <> ToString[multiplier^(-1), FormatType -> StandardForm] <> " m"}]

But I get an awful label.

  1. Could you think of a solution to this problem?

  2. Can anyone think of why conversion to real numbers by "N" is necessary and if not how can it be avoided?

$\endgroup$
  • $\begingroup$ Perhaps "Distance, " <> ToString[N[multiplier^(-1)] can be acceptable. $\endgroup$ – C. E. Jul 30 '13 at 16:02
4
$\begingroup$

You needn't mess around with string manipulation. It can also be done with Row.

multiplier = 10.^11;
func = 10.^-11 x^2;
yScale = ScientificForm[1/multiplier, NumberFormat -> (10^#3 &)];
Plot[func*multiplier, {x, 0, 5}, 
  AxesLabel -> {x, Row[{"Distance, ", yScale, " m"}, BaseStyle -> {FontFamily -> "Arial"}]}]

plot.png

$\endgroup$
  • $\begingroup$ @Nasser Great. It is true that it is easier to use spaces, than Spacer[]. At the same time your yScale definition will not work for you seem to need "N" function, although theoretically it does not seem to be necessary. $\endgroup$ – Dr_Zaszuś Jul 31 '13 at 15:56
  • $\begingroup$ @Szczypawka. Sorry, but don't understand why I need N in yScale. The plot I show in my answer, which was made with my code, shows the correct axis label without using N. $\endgroup$ – m_goldberg Jul 31 '13 at 18:09
  • $\begingroup$ That's true, but just because N is hidden in your code. If you look at your code and at the other answer, you will notice that your equivalent of N is defining multiplier and func initially as real numbers by saying 10.^11, and what I am curious about is that theoretically one doesn't need such conversion. $\endgroup$ – Dr_Zaszuś Aug 1 '13 at 7:31
  • $\begingroup$ @Szczypawka. I wouldn't say N is hidden in my code. I just choose to work with machine real numbers right from the start. It is more efficient, especially when plotting. Further, ScientificForm is designed to work on numbers with head Real. If you insist on the constraint that inexact numbers must be avoided in answers to your question, then my answer is simply bad. But I say such a constraint would be silly when the goal is to make a plot. $\endgroup$ – m_goldberg Aug 1 '13 at 10:11
  • $\begingroup$ Thank you for the opinion. I don't insist that use of N is hidden in your code, and I understand well your desire to work with real numbers. And your answer is good. I use it in my code now. But you see, ideally I multiply my data by 10^11 and not 10.^11, you know, it is mathematically more precise, and I still think that it is very strange that I cannot directly put a natural number there without converting to real ones. But I appreciate your working answer anyway, because it works. :) $\endgroup$ – Dr_Zaszuś Aug 5 '13 at 9:59
3
$\begingroup$

Possibly something like the following:

multiplier = 10^11;
func = 10^-11 x^2;
ylabel = ScientificForm[N@multiplier^(-1), NumberFormat -> (10^#3 &)];

Plot[func*multiplier, {x, 0, 5}, 
    AxesLabel -> {x,"Distance, " <> ToString[ylabel, FormatType ->
    StandardForm] <> " m"}]

enter image description here

This uses ScientificForm and the option NumberFormat to control the appearance.

$\endgroup$
  • $\begingroup$ A good solution, thank you. It seems nevertheless, that "ScientificForm" is not the only possible option in the third string: "NumberForm" works also well. The only doubtful point is that one needs convertion to approximate numbers by "N" which theoretically does not seem to be necessary. But the code would not work without it. $\endgroup$ – Dr_Zaszuś Jul 31 '13 at 15:45
  • $\begingroup$ @Nasser That's true, great: the ToString is not needed. Still the question of using "N" stays. $\endgroup$ – Dr_Zaszuś Jul 31 '13 at 15:51

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.