Problem with tick labels in scripts

Eugen Wintersberger <eugen.wintersberger@...499...>
writes:

The first line is the printing of the ticklabel list before the
pylab.show() command. The second after pylab.show(). Why the list
contains 1 entry before and 7 (as is should) after pylab.show()?

I think matplotlib is deferring the creation of the tick labels because
for all it knows, you might be going to plot something that will cause
the axes to be rescaled and change the tick locations and labels.

I would like to access the labels before the final show command in a
script. But how is this possible with this behavior?

You could set the tick positions and labels explicitly, or if you like
the default tick locator, add the following before getting the ticklabel
objects:

a.xaxis.get_major_locator().refresh()

···

--
Jouni K. Sepp�nen
http://www.iki.fi/jks

Looking back through the archives, I found this discussion of manipulating
ticklabels.

Jouni K. Seppänen wrote:

Eugen Wintersberger <eugen.wintersberger@...499...>
writes:

The first line is the printing of the ticklabel list before the
pylab.show() command. The second after pylab.show(). Why the list
contains 1 entry before and 7 (as is should) after pylab.show()?

I think matplotlib is deferring the creation of the tick labels because
for all it knows, you might be going to plot something that will cause
the axes to be rescaled and change the tick locations and labels.

I would like to access the labels before the final show command in a
script. But how is this possible with this behavior?

You could set the tick positions and labels explicitly, or if you like
the default tick locator, add the following before getting the ticklabel
objects:

a.xaxis.get_major_locator().refresh()

I am having the same problem as Eugen, and the suggested solution of using
a.xaxis.get_major_locator().refresh()
to force the creation of the full set of ticklabels doesn't seem to work for
me.

Sample script:

from pylab import *
#ion()
a =axes([0.2,0.2,0.7,0.7])
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
a.plot(t, s)
a.grid(True)
# matlab handle graphics style
a.xaxis.get_major_locator().refresh()
xticklabels = getp(a, 'xticklabels')
setp(xticklabels, 'color', 'r', fontsize='medium')
setp(xticklabels, dashdirection=0, dashlength=10, dashrotation=90)
#savefig('axprops_demo')
show()

even with a.xaxis.get_major_locator().refresh(), only the first ticklabel
has its position adjusted.

replacing
setp(xticklabels, dashdirection=0, dashlength=10, dashrotation=90)
with code modifying the position
for i in range(len(xticklabels)):
   xtp = getp(xticklabels[i],'position')
   setp(xticklabels[i],position=(xtp[0],-0.05))

also only effects the first ticklabel.

changes to color and fontsize of the first ticklabel do carry over the full
set of ticklabels, but TextWithDash properties do not carry over.

Uncommenting ion() at the beginning of the script generates the desired
image with offset ticklabels, but requires using interactive mode.

Any help either getting
a.xaxis.get_major_locator().refresh()
to work properly, or to get TextWithDash properties to carry over from the
initial single ticklabel to the full set of ticklabels would be greatly
appreciated.

thanks,

Charles Seaton
Research Associate
STC-CMOP
www.stccmop.org

···

--
View this message in context: http://www.nabble.com/Problem-with-tick-labels-in-scripts-tf4260235.html#a12932137
Sent from the matplotlib - users mailing list archive at Nabble.com.

matplotlib creates a prototypical tick (the prototick) and then
creates new ones on as as needed basis, copying properties from the
prototick. Of course, position is one of the properties that cannot
be copied, which is why you are having trouble in your example.
Fortunately, there is an easy solution.

Call ax.xaxis.get_major_ticks() and access the label attribute:

for tick in ax.xaxis.get_major_ticks():
   label = tick.label1

Axis.get_major_ticks will force a call to the locator and update the
tick list. The Axes methods like get_xticklabels are just working on
the existing tick list rather than calling the get_major_ticks method
which is why you are not getting the full list. This is a bug. I
just made changes in svn so that all the accessor methods
(ax.get_xticklines, ax.get_yticklabels, and friends) all trigger a
call to axis.get_major_ticks rather so they should give the same
results going forward.

JDH

FYI, the Tick attributes are:

      tick1line : a Line2D instance
      tick2line : a Line2D instance
      gridline : a Line2D instance
      label1 : a Text instance
      label2 : a Text instance
      gridOn : a boolean which determines whether to draw the tickline
      tick1On : a boolean which determines whether to draw the 1st tickline
      tick2On : a boolean which determines whether to draw the 2nd tickline
      label1On : a boolean which determines whether to draw tick label
      label2On : a boolean which determines whether to draw tick label

···

On 9/27/07, Charles Seaton <cseaton@...1743...> wrote:

I am having the same problem as Eugen, and the suggested solution of using
a.xaxis.get_major_locator().refresh()
to force the creation of the full set of ticklabels doesn't seem to work for
me.