eliminating the top tick on an axis

Hi,

I have a very vexing problem. I am using MPL (both 0.98.1 and 0.91.2)
to create a vertical stack of three plots with no space in between. In
order to do so, I need to eliminate either the top or bottom tick of
each plot. I have chosen to eliminate the top tick, and I am doing
something like the following:

ticks = a.yaxis.get_major_ticks()
ticks[-2].set_visible(False)

where a is the axis object in question. Most of the time, that works,
though I don't know why there is an additional "blank" tick inserted
at the top, making the top most visible tick the second to last
element in the ticks array.. However, sometimes, that deletes the
second tick from the top, leaving the top tick! An example of what I
am talking about can be found at
http://research.jsoishi.org/images/yavg_t_spectra.png. Note that all
of the plots are fine except for the bottom most subpanel of the upper
left plot. All subplots in that figure include exactly the two lines
above.

A simpler test case is the following:

···

---
#!/usr/bin/env python

import pylab as P
import numpy as N

x = N.linspace(0,2*N.pi,1000)
y1 = N.sin(x)
y2 = N.sinc(x)
y3 = N.cos(x)

fig = P.figure()

for i,y in enumerate([y1,y2,y3]):
    bot = 0.05+i*0.3
    ax = fig.add_axes([0.1,bot,0.8,0.3])
    ax.plot(x,y)

    ticks = ax.yaxis.get_major_ticks()
    ticks[-1].set_visible(False) # works on the top & bottom plot
# ticks[-2].set_visible(False) # works on the middle one

    if i != 0:
        ax.set_xticklabels('')

fig.savefig('tick_test.png')

----

For me, the ticks[-1] line gives the image at
http://research.jsoishi.org/images/tick_test.png, while the commented
out ticks[-2] line gives the inverse (the middle plot deletes the top
most tick, and the others delete the second from the last).

Any help would be greatly appreciated.

Jeff

Hi,

It seems that there is no guarantee that the get_major_ticks() methods
returns only ticks within the view interval, i.e., it can return ticks
outside the data limit. As far as I see, this is related with the
general behavior of locator objects.

For example,

In [71]: ax.yaxis.get_view_interval()
Out[71]: array([-0.4, 1.2])

In [72]: ax.yaxis.major.locator()
Out[72]: array([-0.4, -0.2, 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4])

So, for the middle axes in your example script, the last tick actually
corresponds to 1.4 not 1.2.
I guess this is not a bug though.
And the draw() method actually checks and only draw those within the
view interval.

And for your purpose, you may use following function to only select
the ticks within the view interval.

import matplotlib.transforms as mtransforms

def get_major_ticks_within_view_interval(axis):

    interval = axis.get_view_interval()

    ticks_in_view_interval = []
    for tick, loc in zip(axis.get_major_ticks(),
                         axis.get_major_locator()()):
        if mtransforms.interval_contains(interval, loc):
            ticks_in_view_interval.append(tick)

    return ticks_in_view_interval

And, in your example script, use this function as below instead of
get_major_ticks() methods.

   ticks = get_major_ticks_within_view_interval(ax.yaxis)

Also, I guess it'd better to have only the label invisible.

   ticks[-1].label.set_visible(False)\

While I don't think this is a best solution, it seems work okay.
I hope this is helpful.

-JJ

···

On Thu, Sep 11, 2008 at 6:41 PM, j s oishi <jsoishi@...287...> wrote:

Hi,

I have a very vexing problem. I am using MPL (both 0.98.1 and 0.91.2)
to create a vertical stack of three plots with no space in between. In
order to do so, I need to eliminate either the top or bottom tick of
each plot. I have chosen to eliminate the top tick, and I am doing
something like the following:

ticks = a.yaxis.get_major_ticks()
ticks[-2].set_visible(False)

where a is the axis object in question. Most of the time, that works,
though I don't know why there is an additional "blank" tick inserted
at the top, making the top most visible tick the second to last
element in the ticks array.. However, sometimes, that deletes the
second tick from the top, leaving the top tick! An example of what I
am talking about can be found at
http://research.jsoishi.org/images/yavg_t_spectra.png. Note that all
of the plots are fine except for the bottom most subpanel of the upper
left plot. All subplots in that figure include exactly the two lines
above.

A simpler test case is the following:

---
#!/usr/bin/env python

import pylab as P
import numpy as N

x = N.linspace(0,2*N.pi,1000)
y1 = N.sin(x)
y2 = N.sinc(x)
y3 = N.cos(x)

fig = P.figure()

for i,y in enumerate([y1,y2,y3]):
   bot = 0.05+i*0.3
   ax = fig.add_axes([0.1,bot,0.8,0.3])
   ax.plot(x,y)

   ticks = ax.yaxis.get_major_ticks()
   ticks[-1].set_visible(False) # works on the top & bottom plot
# ticks[-2].set_visible(False) # works on the middle one

   if i != 0:
       ax.set_xticklabels('')

fig.savefig('tick_test.png')

----

For me, the ticks[-1] line gives the image at
http://research.jsoishi.org/images/tick_test.png, while the commented
out ticks[-2] line gives the inverse (the middle plot deletes the top
most tick, and the others delete the second from the last).

Any help would be greatly appreciated.

Jeff

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users