empty xticklabel before plotting ...

Hi Everyone,

I don’t like the default scientific formatting in matplotlib, IMHO the format of having a zero after the exponent is a waste of space in my opinion …
What I mean is that mpl is writing zero as:

0.0e+00 or 1.2e-03. IMHO it would suffice just to do 0.0 and 1.2e-3, which take less space and looks better (again, imho).

In any case, I would like to access the strings of the yticklables in my plots and I discovered I can do the following:

setp(axs[2].set_yticklabels([‘0.0’,‘2.0E-4’,‘4.0E-4’,‘6.0E-4’,‘8.0E-4’,‘1.0E-3’,‘1.2E-3’]))

Which works, but forces me to first look at the output and the redraw everything with manually feeding the values.

if there is a way to loop on this text values before plotting I will be happy to know.

I tried doing this:
In [1]: from pylab import *

In [2]: f=figure()

In [3]: ax=f.add_subplot()

In [4]: ax.plot([1,2,3,4,5])

···

AttributeError Traceback (most recent call last)

/home/oz/ in ()

AttributeError: ‘NoneType’ object has no attribute ‘plot’

In [5]: ax=f.add_subplot(111)

In [6]: ax.plot([1,2,3,4,5])
Out[6]: [<matplotlib.lines.Line2D object at 0x95ecdcc>]

In [7]: a=ax.get_xticklabels()

In [8]: a[0].get_text()
Out[8]: ‘’

In [9]: draw()

In [10]: a[0].get_text()
In [10]: a[0].get_text()
Out[10]: ‘’

In [11]: show()

KeyboardInterrupt:

In [12]: a[0].get_text()

Out[12]: u’0.0

As can be seen, the text string is empty before calling show, which is forcing me to show the image . Is there a way to access these labales with out calling show() ?

Thanks in advance,


Oz Nahum
Graduate Student
Zentrum für Angewandte Geologie
Universität Tübingen


Imagine there’s no countries
it isn’t hard to do
Nothing to kill or die for
And no religion too

Imagine all the people
Living life in peace

Hi Everyone,

I don't like the default scientific formatting in matplotlib, IMHO the
format of having a zero after the exponent is a waste of space in my opinion
...
What I mean is that mpl is writing zero as:
0.0e+00 or 1.2e-03. IMHO it would suffice just to do 0.0 and 1.2e-3, which
take less space and looks better (again, imho).

<SNIP>

As can be seen, the text string is empty before calling show, which is
forcing me to show the image . Is there a way to access these labales with
out calling show() ?

What you want is to set a custom formatter:

http://matplotlib.sourceforge.net/api/ticker_api.html

import matplotlib.pyplot as plt
f = plt.figure()
ax = f.add_subplot(111)
ax.plot([1,2,3,4,5])
# Formats ticks on the xaxis with the %.2g format string
ax.xaxis.set_major_formatter(plt.FormatStrFormatter('%.2g'))
plt.show()

If you can't get what you want using a format string, you can write a
function that does what you want can create a formatter from that
using FuncFormatter.

Ryan

···

On Thu, Sep 23, 2010 at 3:49 AM, Oz Nahum <nahumoz@...287...> wrote:

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

Hi Ryan,

Thanks for your answer. However, I don’t understand from the existing documentation how to use tickers.

In the past I used the following method:
class SciFormatter(Formatter):

def __call__(self, x, pos=None):
    return "%1.1e" % x

axs.yaxis.set_major_formatter(SciFormatter())

This still does not allow me to get read of the zeros that matplotlib coherces .

I would like to manipulate the strings of the tick labels directly. I know it’s not ideal, but I it should work.

If I could access the label “1.1E+01”, I could tell python just to take label[:-2] and glue it to label[-1], which will give me

“1.1E+1”

I would be greatful if someone showed me how to access that string.

Cheers,
Oz

···

On Thu, Sep 23, 2010 at 3:37 PM, Ryan May <rmay31@…287…> wrote:

On Thu, Sep 23, 2010 at 3:49 AM, Oz Nahum <nahumoz@…287…> wrote:

Hi Everyone,

I don’t like the default scientific formatting in matplotlib, IMHO the

format of having a zero after the exponent is a waste of space in my opinion

What I mean is that mpl is writing zero as:

0.0e+00 or 1.2e-03. IMHO it would suffice just to do 0.0 and 1.2e-3, which

take less space and looks better (again, imho).

As can be seen, the text string is empty before calling show, which is

forcing me to show the image . Is there a way to access these labales with

out calling show() ?

What you want is to set a custom formatter:

http://matplotlib.sourceforge.net/api/ticker_api.html

import matplotlib.pyplot as plt

f = plt.figure()
ax = f.add_subplot(111)

ax.plot([1,2,3,4,5])

Formats ticks on the xaxis with the %.2g format string

ax.xaxis.set_major_formatter(plt.FormatStrFormatter(’%.2g’))

plt.show()

If you can’t get what you want using a format string, you can write a

function that does what you want can create a formatter from that

using FuncFormatter.

Ryan

Ryan May

Graduate Research Assistant

School of Meteorology

University of Oklahoma


Oz Nahum
Graduate Student
Zentrum für Angewandte Geologie
Universität Tübingen


Imagine there’s no countries
it isn’t hard to do

Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

Hi Every, Hi Ryan

I finally solved this issue, which bothered me very long.

I managed to make a nicer scientific formatting on the xticks !

from pylab import *
import numpy as N

from matplotlib.ticker import Formatter,FuncFormatter
import os
#class to produce scientific format numbering
class SciFormatter(Formatter):
def call(self, x, pos=None):
return “%1.e” % x

class NiceSciFormatter(Formatter):
def call(self, x, pos=None):
a="%1.1E" % x
a=a[:5]+a[-1]
return a

fig=figure()
ax=fig.add_subplot(111)

y=[.0001,.0002,.0003,.0004,.0005]
x=[1,2,3,4,5]
ax.plot(x,y)

ax.yaxis.set_major_formatter(NiceSciFormatter())

show()

Cheers ,

Oz

···


Oz Nahum
Graduate Student

Zentrum für Angewandte Geologie
Universität Tübingen


Imagine there’s no countries
it isn’t hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

Why not just format the number yourself?

import matplotlib.pyplot as plt
import numpy as np

def format(x, pos=None):
    if x == 0.0:
        exp = 0
    else:
        exp = int(np.log10(np.abs(x)))
    mant = x / 10**exp
    return '%.2fE%+d' % (mant, exp)

f = plt.figure()
ax = f.add_subplot(111)
data = np.array([1,2,3,4,5]) / 100.
ax.plot(data, np.arange(len(data)))

ax.xaxis.set_major_formatter(plt.FuncFormatter(format))

plt.show()

Ryan

···

On Thu, Sep 23, 2010 at 8:52 AM, Oz Nahum <nahumoz@...287...> wrote:

Hi Ryan,

Thanks for your answer. However, I don't understand from the existing
documentation how to use tickers.

In the past I used the following method:
class SciFormatter(Formatter):
def __call__(self, x, pos=None):
return "%1.1e" % x

axs.yaxis.set_major_formatter(SciFormatter())

This still does not allow me to get read of the zeros that matplotlib
coherces .

I would like to manipulate the strings of the tick labels directly. I know
it's not ideal, but I it should work.

If I could access the label "1.1E+01", I could tell python just to take
label[:-2] and glue it to label[-1], which will give me
"1.1E+1"

I would be greatful if someone showed me how to access that string.

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

Hi Ryan,
Thanks again,

Well your solution is also working.

I get very similar results with

class NiceSciFormatter(Formatter):

def __call__(self, x, pos=None):

a="%1.1E" % x
a=a[:5]+a[-1]
return a

It seems though that your solution enables more fine tuning !

Cheers,
Oz

···

On Thu, Sep 23, 2010 at 5:10 PM, Ryan May <rmay31@…287…> wrote:

On Thu, Sep 23, 2010 at 8:52 AM, Oz Nahum <nahumoz@…287…> wrote:

Hi Ryan,

Thanks for your answer. However, I don’t understand from the existing

documentation how to use tickers.

In the past I used the following method:

class SciFormatter(Formatter):

def __call__(self, x, pos=None):
    return "%1.1e" % x

axs.yaxis.set_major_formatter(SciFormatter())

This still does not allow me to get read of the zeros that matplotlib

coherces .

I would like to manipulate the strings of the tick labels directly. I know

it’s not ideal, but I it should work.

If I could access the label “1.1E+01”, I could tell python just to take

label[:-2] and glue it to label[-1], which will give me

“1.1E+1”

I would be greatful if someone showed me how to access that string.

Why not just format the number yourself?

import matplotlib.pyplot as plt

import numpy as np

def format(x, pos=None):

if x == 0.0:

    exp = 0

else:

    exp = int(np.log10(np.abs(x)))

mant = x / 10**exp

return '%.2fE%+d' % (mant, exp)

f = plt.figure()

ax = f.add_subplot(111)

data = np.array([1,2,3,4,5]) / 100.

ax.plot(data, np.arange(len(data)))

ax.xaxis.set_major_formatter(plt.FuncFormatter(format))

plt.show()

Ryan

Ryan May

Graduate Research Assistant

School of Meteorology

University of Oklahoma


Oz Nahum
Graduate Student
Zentrum für Angewandte Geologie
Universität Tübingen


Imagine there’s no countries
it isn’t hard to do

Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace