tick label hiding problem

Hi all,

    > I'm still having trouble figuring out what's going on
    > with tick labels. I can't figure out why
    > Axes.get_{x,y}ticklabels() is returning a list of length
    > unity when I expect to have the list of all labels.

    > If I run the code below (test.py), I expect a list of 9 x
    > tick labels. If I run the code once ('run test' in
    > ipython), then it gives me a list of 1 tick label. If I
    > close the interactive window it generates and run it
    > again, it again returns 1 tick label. However, if I
    > leave the window open and re-run the test, it gives the
    > right answer.

    > Can anyone else reproduce this? Is there something I'm
    > missing? I'm using svn rev 2730 of matplotlib, and I
    > think this is new behavior in the last couple months.

It's a feature, not a bug :slight_smile:

Here is what is happening. matplotlib allocates a different number of
ticks depending on the zoom level, and the axis seeds itself with a
single tick called the "protoTick". If you zoom to the right and new
ticks need to be created, the properties from the protoTick are copied
to the newly created ticks. 99% of the time, this is what you want.
If you have large bold tick labels, and zoom to the right, you want
the new ticks that appear to be large and bold. So this explains why
you see a single tick before the window is drawn (the protoTick), a
list of ticks after the window is drawn, and why the properties of the
first tick (the "visible" property in this case) are transferred to
the other ticks.

So: how do you solve your problem, of making the first tick invisible?
What I do when I need to solve this problem, which comes up a lot with
multiple axes where ticks can overlap, is the following

from matplotlib.ticker import ScalarFormatter

class MyFormatter(ScalarFormatter):
    def __call__(self, x, pos=None):
        if pos==0: return ''
        else: return ScalarFormatter(self, x, pos)

ax.xaxis.set_major_formatter(MyFormatter())

I often want to do this for the last tick as well, but there is
currently no convenient way to do this, as the tick locator produces
the tick location list and the tick formatter doesn't know how many of
them there are. I've been considering modifying the tick locator code
to pass -1 for the pos for the last tick to solve just this use case.
If others need this or think it desirable, just give me a nudge and
I'll do it.

Details about the tick locators and formatter can be found at
http://matplotlib.sourceforge.net/matplotlib.ticker.html and there are
a number of examples

  ~/mpl/examples> grep -l Formatter *.py
  custom_ticker1.py
  dashtick.py
  date_demo1.py
  date_demo2.py
  date_demo_rrule.py
  finance_demo.py
  major_minor_demo1.py
  major_minor_demo2.py
  newscalarformatter_demo.py
  shared_axis_demo.py

Hope this helps,
JDH

Hi John,

Hmm.... I have 3 use cases where I want to isolate individual tick labels and make them invisible:

1) the first tick (0; for stacked plots)
2) the last tick (-1; for stacked plots)
3) every other tick for the last few ticks (e.g. -2, -4, -6; for a log-scale axis with large tick labels, which can overlap on one end)

Is there a way to force some sort of pseudo-draw event, such that the sizing is done and the ticks are created from the protoTick, but the draw isn't actually performed by the backend? That way one could set the visibility property rather than wrap the Formatter.

Thanks,
Mike

···

On Aug 31, 2006, at 4:36 AM, John Hunter wrote:

So: how do you solve your problem, of making the first tick invisible?
What I do when I need to solve this problem, which comes up a lot with
multiple axes where ticks can overlap, is the following

from matplotlib.ticker import ScalarFormatter

class MyFormatter(ScalarFormatter):
    def __call__(self, x, pos=None):
        if pos==0: return ''
        else: return ScalarFormatter(self, x, pos)

ax.xaxis.set_major_formatter(MyFormatter())

I often want to do this for the last tick as well, but there is
currently no convenient way to do this, as the tick locator produces
the tick location list and the tick formatter doesn't know how many of
them there are. I've been considering modifying the tick locator code
to pass -1 for the pos for the last tick to solve just this use case.
If others need this or think it desirable, just give me a nudge and
I'll do it.