working with ticklabels text in matplolib

I have a problem with changing the ticklabels text. In fact I am aware of the
method which is explained by the matplotlib help center. But I need more
flexibility with ticklabels text.
For example, I want to add an "a" before every tick label of the xaxis. So I
wrote the following sample code:

···

#----------------------------------------------------------------
from pylab import *
#
t = arange(0.0, 100.0, 0.1)
s = sin(0.1*pi*t)*exp(-t*0.01)
#
ax = subplot(111)
plot(t,s)
#
for label1 in ax.xaxis.get_majorticklabels():
    label1.set_weight('bold')
    label1._text="a"+label1._text
#
show()
#----------------------------------------------------------------
It seems to me that `` label1._text="a"+label1._text ' ' should do this
job, but it does nothing. The only way I found was using something like
this:

ax.xaxis.set_ticklabels(('a0','a20','a40','a60','a80','a100'))

which I would rather not to use, because there are lots of graphs in my
project and I do not want to this process manually. So, my question is which
property or method would set (and also get) the ticklabel text?

Thanks is advance
--
View this message in context: http://old.nabble.com/working-with-ticklabels-text-in-matplolib-tp32341717p32341717.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Why not use get_ticklabels() to get the list of strings, modify that, and call set_ticklabels() with the modified list?

Ben Root

···

On Friday, August 26, 2011, a.sam <a.samii@…287…> wrote:

I have a problem with changing the ticklabels text. In fact I am aware of the
method which is explained by the matplotlib help center. But I need more

flexibility with ticklabels text.
For example, I want to add an “a” before every tick label of the xaxis. So I
wrote the following sample code:
#----------------------------------------------------------------

from pylab import *

t = arange(0.0, 100.0, 0.1)
s = sin(0.1pit)exp(-t0.01)

ax = subplot(111)
plot(t,s)

for label1 in ax.xaxis.get_majorticklabels():

label1.set_weight(‘bold’)
label1._text=“a”+label1._text

show()
#----------------------------------------------------------------
It seems to me that `` label1._text=“a”+label1._text ’ ’ should do this

job, but it does nothing. The only way I found was using something like
this:

ax.xaxis.set_ticklabels((‘a0’,‘a20’,‘a40’,‘a60’,‘a80’,‘a100’))

which I would rather not to use, because there are lots of graphs in my
project and I do not want to this process manually. So, my question is which
property or method would set (and also get) the ticklabel text?

Thanks is advance

Hi,

I have a problem with changing the ticklabels text. In fact I am aware of the
method which is explained by the matplotlib help center. But I need more
flexibility with ticklabels text.

You could set the formatter yourself instead of manipulating strings or
setting the ticklabels by hand. IE:

my_formatter = matplotlib.ticker.FormatStrFormatter('a%s')
#etc... see http://matplotlib.sourceforge.net/api/ticker_api.html

a = plt.gca()
a.xaxis.set_major_formatter(my_formatter)

plt.draw()

Regards,

Sebastian

···

On Fri, 2011-08-26 at 06:09 -0700, a.sam wrote:

For example, I want to add an "a" before every tick label of the xaxis. So I
wrote the following sample code:
#----------------------------------------------------------------
from pylab import *
#
t = arange(0.0, 100.0, 0.1)
s = sin(0.1*pi*t)*exp(-t*0.01)
#
ax = subplot(111)
plot(t,s)
#
for label1 in ax.xaxis.get_majorticklabels():
    label1.set_weight('bold')
    label1._text="a"+label1._text
#
show()
#----------------------------------------------------------------
It seems to me that `` label1._text="a"+label1._text ' ' should do this
job, but it does nothing. The only way I found was using something like
this:

ax.xaxis.set_ticklabels(('a0','a20','a40','a60','a80','a100'))

which I would rather not to use, because there are lots of graphs in my
project and I do not want to this process manually. So, my question is which
property or method would set (and also get) the ticklabel text?

Thanks is advance