Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
Perhaps "Distance, " <> ToString[N[multiplier^(-1)] can be acceptable. –  Pickett Jul 30 '13 at 16:02
add comment

2 Answers

up vote 3 down vote accepted

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

share|improve this answer
    
@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. –  Szczypawka Jul 31 '13 at 15:56
    
@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. –  m_goldberg Jul 31 '13 at 18:09
    
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. –  Szczypawka Aug 1 '13 at 7:31
    
@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. –  m_goldberg Aug 1 '13 at 10:11
    
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. :) –  Szczypawka Aug 5 '13 at 9:59
add comment

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.

share|improve this answer
    
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. –  Szczypawka Jul 31 '13 at 15:45
    
@Nasser That's true, great: the ToString is not needed. Still the question of using "N" stays. –  Szczypawka Jul 31 '13 at 15:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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