scientific notation in ticklabels for linear plot

Hi,

I'm wondering if there's some relatively automatic way to have the
ticklabels to come out in scientific notation for an axis that uses a
linear scale (and has a range that warrants scientific notation)? For
example, an axis that goes from 0 to 2.E18 by default uses the labels 0,
0.5, 1.0, 1.5, 2.0 and puts 1e18 at the end of the axis. To me this is
unappealing. 1e18 is a computer programming way to write the 10^{18} (in
LaTeX formatting). In IDL the ticklabels are 5.0x10^{17}, 1.0x10^{18},
1.5x10^{18}, 2.0x10^{18}. This is one instance where I think IDL gets
it right and matplotlib gets it wrong. So, as far as I can tell, one
can give the ticklabels by hand, and so I could achieve my desired
labels that way, but it'd be nice to have a more automatic way to do it.
Searching the examples, I've come up empty so far. I would also
advocate changing the default format for labeling axes that fall in this
category. Do the developers have any opinions on this?

Regards,
Jon

2010/10/18 Jonathan Slavin <jslavin@...1081...>:

I'm wondering if there's some relatively automatic way to have the
ticklabels to come out in scientific notation for an axis that uses a
linear scale (and has a range that warrants scientific notation)? For
example, an axis that goes from 0 to 2.E18 by default uses the labels 0,
0.5, 1.0, 1.5, 2.0 and puts 1e18 at the end of the axis. To me this is
unappealing. 1e18 is a computer programming way to write the 10^{18} (in
LaTeX formatting). In IDL the ticklabels are 5.0x10^{17}, 1.0x10^{18},
1.5x10^{18}, 2.0x10^{18}. This is one instance where I think IDL gets
it right and matplotlib gets it wrong. So, as far as I can tell, one
can give the ticklabels by hand, and so I could achieve my desired
labels that way, but it'd be nice to have a more automatic way to do it.
Searching the examples, I've come up empty so far. I would also
advocate changing the default format for labeling axes that fall in this
category. Do the developers have any opinions on this?

There's a thread some monthes ago about this, I provided code to do
this. Currently, this Formatter class didn't make it into matplotlib
due a lack of providing a patch by me. If others agree that this
would be a good feature, I'll clean the code if necessary and provide
another patch for this.

Looking for the thread ...
http://article.gmane.org/gmane.comp.python.matplotlib.general/23824/match=scientific+notation+friedrich

IIRC, the formatter class does NOT provide automatic choice of
appropriate exponent, i.e. you have to provide this in instantiation
time.

hth, and let me know about any progress,
Friedrich

P.S.: I just gave the thread link and didn't look into it further.
But it should be the message I remembered.

I agree with Jonathan and would very much like to see this feature implemented. The example shown in the thread didn't show the "×" symbol, however, which would be nice to have -- e.g. it should read 2.0 × 10² rather than 2.0 10².

David

···

On Oct 19, 2010, at 1:08 PM, Friedrich Romstedt wrote:

2010/10/18 Jonathan Slavin <jslavin@...1081...>:

I'm wondering if there's some relatively automatic way to have the
ticklabels to come out in scientific notation for an axis that uses a
linear scale (and has a range that warrants scientific notation)? For
example, an axis that goes from 0 to 2.E18 by default uses the labels 0,
0.5, 1.0, 1.5, 2.0 and puts 1e18 at the end of the axis. To me this is
unappealing. 1e18 is a computer programming way to write the 10^{18} (in
LaTeX formatting). In IDL the ticklabels are 5.0x10^{17}, 1.0x10^{18},
1.5x10^{18}, 2.0x10^{18}. This is one instance where I think IDL gets
it right and matplotlib gets it wrong. So, as far as I can tell, one
can give the ticklabels by hand, and so I could achieve my desired
labels that way, but it'd be nice to have a more automatic way to do it.
Searching the examples, I've come up empty so far. I would also
advocate changing the default format for labeling axes that fall in this
category. Do the developers have any opinions on this?

There's a thread some monthes ago about this, I provided code to do
this. Currently, this Formatter class didn't make it into matplotlib
due a lack of providing a patch by me. If others agree that this
would be a good feature, I'll clean the code if necessary and provide
another patch for this.

Looking for the thread ...
http://article.gmane.org/gmane.comp.python.matplotlib.general/23824/match=scientific+notation+friedrich

IIRC, the formatter class does NOT provide automatic choice of
appropriate exponent, i.e. you have to provide this in instantiation
time.

hth, and let me know about any progress,
Friedrich

P.S.: I just gave the thread link and didn't look into it further.
But it should be the message I remembered.

------------------------------------------------------------------------------
Download new Adobe(R) Flash(R) Builder(TM) 4
The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly
Flex(R) Builder(TM)) enable the development of rich applications that run
across multiple browsers and platforms. Download your free trials today!
http://p.sf.net/sfu/adobe-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

What about inserting \cdot, that's the scientific notation I do prefer?

If I'm not mistaken that's what I did that time, might be unreadable
in the preview? I checked, when you look close you see the dot in
gmane preview.

We can make this customisable, with \times as an alternative option.

Friedrich

I like the times symbol but others prefer the dot (which I missed in the gmane preview!). So I like your suggestion of providing an option to use either \cdot or \times.

David

···

On Oct 19, 2010, at 3:23 PM, Friedrich Romstedt wrote:

What about inserting \cdot, that's the scientific notation I do prefer?

If I'm not mistaken that's what I did that time, might be unreadable
in the preview? I checked, when you look close you see the dot in
gmane preview.

We can make this customisable, with \times as an alternative option.

Friedrich

2010/10/19 David Pine <djpine@...287...>:

I like the times symbol but others prefer the dot (which I missed in the gmane preview!). So I like your suggestion of providing an option to use either \cdot or \times.

Okay, I'll try to look into it next week, is that okay with you both?
I don't want to do it now since there might be more under the hood,
just thinking about automatic exponent choice, I have a module for
that, but it needs to pick up all the range the formatter spans over.

I'd say we also have use for three other configs of the exponent choice:
a) largest
b) smallest
c) mean
d) dynamic
exponent.

Examples:
(a) 0.001 10^2 ... 2.0 10^2
(b) 1.0 10^-1 ... 200.0 10^-1
(c) 0.01 10^1 ... 20.0 10^1
(d) 1.0 10^-1 ... 2.0 10^2
Sorry if there's some mistake but you see the principle.

Also the number of digits present need to be configurable ... enough for a week.

I favour (c) as the default, and 2 digits precision.

Friedrich

I think that'd be fine -- i.e. the option of \cdot or \times (though in
the gmane preview the dot looks a bit low). In the mean time, I came up
with the method below that worked for my purpose.

Jon

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter

def scinot(x,pos=None):
    if x == 0:
        s = '0'
    else:
        xp = int(np.floor(np.log10(np.abs(x))))
        mn = x/10.**xp
        # Here we truncate to 2 significant digits -- may not be enough
        # in all cases
        s = '$'+str('%.1f'%mn) +'\\times 10^{'+str(xp)+'}$'
    return s

x = np.linspace(0.,2.,10)*1.E18
y = 2.*(x/1.E18) - 1.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
ax.xaxis.set_major_formatter(FucFormatter(scinot))
plt.show()

···

On Tue, 2010-10-19 at 15:30 -0400, David Pine wrote:

I like the times symbol but others prefer the dot (which I missed in the gmane preview!). So I like your suggestion of providing an option to use either \cdot or \times.

David

On Oct 19, 2010, at 3:23 PM, Friedrich Romstedt wrote:

> What about inserting \cdot, that's the scientific notation I do prefer?
>
> If I'm not mistaken that's what I did that time, might be unreadable
> in the preview? I checked, when you look close you see the dot in
> gmane preview.
>
> We can make this customisable, with \times as an alternative option.
>
> Friedrich

--
______________________________________________________________
Jonathan D. Slavin Harvard-Smithsonian CfA
jslavin@...1081... 60 Garden Street, MS 83
phone: (617) 496-7981 Cambridge, MA 02138-1516
cell: (781) 363-0035 USA
______________________________________________________________

2010/10/19 Jonathan Slavin <jslavin@...1081...>:

I think that'd be fine -- i.e. the option of \cdot or \times (though in
the gmane preview the dot looks a bit low). In the mean time, I came up
with the method below that worked for my purpose.

Okay thx

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter

def scinot(x,pos=None):
if x == 0:
s = '0'
else:
xp = int(np.floor(np.log10(np.abs(x))))
mn = x/10.**xp
# Here we truncate to 2 significant digits -- may not be enough
# in all cases
s = '$'+str('%.1f'%mn) +'\\times 10^{'+str(xp)+'}$'
return s

x = np.linspace(0.,2.,10)*1.E18
y = 2.*(x/1.E18) - 1.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
ax.xaxis.set_major_formatter(FucFormatter(scinot))

typo for the records although abvious: FuncFormatter

plt.show()

Okay I can do the tedious work of changing my code ASAP but with low
priority ...

Friedrich

Friedrich,

Our e-mails crossed. I don't think the numbers need to have the same
exponent. I would go with (d) as my example does. The more difficult
part to my mind is the number of significant digits to use. The current
code that determines whether to use an offset or not must look at the
number of significant digits needed to represent the ticklabels. To me
what would be optimal is to, by default, use scientific notation (when
the ticklabel numbers are larger or smaller than some range, say 10^3
and 10^-3) except when an offset is called for, say more than 3
significant digits are needed to represent the ticklabel numbers in
scientific notation.

Jon

···

On Tue, 2010-10-19 at 21:41 +0200, Friedrich Romstedt wrote:

2010/10/19 David Pine <djpine@...287...>:
> I like the times symbol but others prefer the dot (which I missed in the gmane preview!). So I like your suggestion of providing an option to use either \cdot or \times.

Okay, I'll try to look into it next week, is that okay with you both?
I don't want to do it now since there might be more under the hood,
just thinking about automatic exponent choice, I have a module for
that, but it needs to pick up all the range the formatter spans over.

I'd say we also have use for three other configs of the exponent choice:
a) largest
b) smallest
c) mean
d) dynamic
exponent.

Examples:
(a) 0.001 10^2 ... 2.0 10^2
(b) 1.0 10^-1 ... 200.0 10^-1
(c) 0.01 10^1 ... 20.0 10^1
(d) 1.0 10^-1 ... 2.0 10^2
Sorry if there's some mistake but you see the principle.

Also the number of digits present need to be configurable ... enough for a week.

I favour (c) as the default, and 2 digits precision.

Friedrich

--
______________________________________________________________
Jonathan D. Slavin Harvard-Smithsonian CfA
jslavin@...1081... 60 Garden Street, MS 83
phone: (617) 496-7981 Cambridge, MA 02138-1516
cell: (781) 363-0035 USA
______________________________________________________________